g_string_replace: Don't replace empty string more than once per location

This matches the behaviour of Python `str.replace()`, and avoids carrying
out 2**32 replacements before n wraps around, which is almost certainly
not what we want.

Resolves: https://gitlab.gnome.org/GNOME/glib/-/issues/2452
Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Simon McVittie 2021-08-02 11:51:56 +01:00
parent 7d35e49c42
commit 0a8c7e57ab

View File

@ -995,6 +995,15 @@ g_string_replace (GString *string,
g_string_insert (string, pos, replace);
cur = string->str + pos + r_len;
n++;
/* Only match the empty string once at any given position, to
* avoid infinite loops */
if (f_len == 0)
{
if (cur[0] == '\0')
break;
else
cur++;
}
if (n == limit)
break;
}