Merge branch 'string-overflow-check' into 'main'

gstring: Fix overflow check when expanding the string

See merge request GNOME/glib!4655
This commit is contained in:
Philip Withnall
2025-06-03 12:07:10 +00:00

View File

@@ -68,10 +68,6 @@ static void
g_string_expand (GString *string,
gsize len)
{
/* Detect potential overflow */
if G_UNLIKELY ((G_MAXSIZE - string->len - 1) < len)
g_error ("adding %" G_GSIZE_FORMAT " to string would overflow", len);
string->allocated_len = g_nearest_pow (string->len + len + 1);
/* If the new size is bigger than G_MAXSIZE / 2, only allocate enough
* memory for this string and don't over-allocate.
@@ -86,6 +82,10 @@ static inline void
g_string_maybe_expand (GString *string,
gsize len)
{
/* Detect potential overflow */
if G_UNLIKELY ((G_MAXSIZE - string->len - 1) < len)
g_error ("adding %" G_GSIZE_FORMAT " to string would overflow", len);
if (G_UNLIKELY (string->len + len >= string->allocated_len))
g_string_expand (string, len);
}