Merge branch 'cherry-pick-cc647f9e' into 'glib-2-84'

Backport "gstring: carefully handle gssize parameters"

See merge request GNOME/glib!4614
This commit is contained in:
Marco Trevisan 2025-04-29 02:37:52 +00:00
commit e2ed5efc0e

View File

@ -480,8 +480,9 @@ g_string_insert_len (GString *string,
return string; return string;
if (len < 0) if (len < 0)
len = strlen (val); len_unsigned = strlen (val);
len_unsigned = len; else
len_unsigned = len;
if (pos < 0) if (pos < 0)
pos_unsigned = string->len; pos_unsigned = string->len;
@ -778,10 +779,12 @@ g_string_insert_c (GString *string,
g_string_maybe_expand (string, 1); g_string_maybe_expand (string, 1);
if (pos < 0) if (pos < 0)
pos = string->len; pos_unsigned = string->len;
else else
g_return_val_if_fail ((gsize) pos <= string->len, string); {
pos_unsigned = pos; pos_unsigned = pos;
g_return_val_if_fail (pos_unsigned <= string->len, string);
}
/* If not just an append, move the old stuff */ /* If not just an append, move the old stuff */
if (pos_unsigned < string->len) if (pos_unsigned < string->len)
@ -814,6 +817,7 @@ g_string_insert_unichar (GString *string,
gssize pos, gssize pos,
gunichar wc) gunichar wc)
{ {
gsize pos_unsigned;
gint charlen, first, i; gint charlen, first, i;
gchar *dest; gchar *dest;
@ -855,15 +859,18 @@ g_string_insert_unichar (GString *string,
g_string_maybe_expand (string, charlen); g_string_maybe_expand (string, charlen);
if (pos < 0) if (pos < 0)
pos = string->len; pos_unsigned = string->len;
else else
g_return_val_if_fail ((gsize) pos <= string->len, string); {
pos_unsigned = pos;
g_return_val_if_fail (pos_unsigned <= string->len, string);
}
/* If not just an append, move the old stuff */ /* If not just an append, move the old stuff */
if ((gsize) pos < string->len) if (pos_unsigned < string->len)
memmove (string->str + pos + charlen, string->str + pos, string->len - pos); memmove (string->str + pos_unsigned + charlen, string->str + pos_unsigned, string->len - pos_unsigned);
dest = string->str + pos; dest = string->str + pos_unsigned;
/* Code copied from g_unichar_to_utf() */ /* Code copied from g_unichar_to_utf() */
for (i = charlen - 1; i > 0; --i) for (i = charlen - 1; i > 0; --i)
{ {
@ -921,6 +928,7 @@ g_string_overwrite_len (GString *string,
const gchar *val, const gchar *val,
gssize len) gssize len)
{ {
gsize len_unsigned;
gsize end; gsize end;
g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (string != NULL, NULL);
@ -932,14 +940,16 @@ g_string_overwrite_len (GString *string,
g_return_val_if_fail (pos <= string->len, string); g_return_val_if_fail (pos <= string->len, string);
if (len < 0) if (len < 0)
len = strlen (val); len_unsigned = strlen (val);
else
len_unsigned = len;
end = pos + len; end = pos + len_unsigned;
if (end > string->len) if (end > string->len)
g_string_maybe_expand (string, end - string->len); g_string_maybe_expand (string, end - string->len);
memcpy (string->str + pos, val, len); memcpy (string->str + pos, val, len_unsigned);
if (end > string->len) if (end > string->len)
{ {