mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-11-08 10:26:16 +01:00
Add overflow protection to g_string_maybe_expand()
This commit is contained in:
parent
d01dc6d23a
commit
b5447e8e35
@ -76,9 +76,17 @@ static void
|
|||||||
g_string_maybe_expand (GString *string,
|
g_string_maybe_expand (GString *string,
|
||||||
gsize len)
|
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 (string->len + len >= string->allocated_len)
|
if (string->len + len >= string->allocated_len)
|
||||||
{
|
{
|
||||||
string->allocated_len = g_nearest_pow (string->len + len + 1);
|
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. */
|
||||||
|
if (string->allocated_len == 0)
|
||||||
|
string->allocated_len = string->len + len + 1;
|
||||||
string->str = g_realloc (string->str, string->allocated_len);
|
string->str = g_realloc (string->str, string->allocated_len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user