From: Bruno Haible Date: Fri, 11 Apr 2025 14:45:03 +0000 (+0200) Subject: vasnprintf: Fix memory size bound for %g with grouping and precision. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=710d9989be46b50b02dd9290215fb16ec124af18;p=gnulib.git vasnprintf: Fix memory size bound for %g with grouping and precision. * lib/vasnprintf.c (MAX_ROOM_NEEDED): For %g, consider also the size of the thousands separators. --- diff --git a/ChangeLog b/ChangeLog index 6e4e2bbe21..4a9415a1ff 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2025-04-11 Bruno Haible + + vasnprintf: Fix memory size bound for %g with grouping and precision. + * lib/vasnprintf.c (MAX_ROOM_NEEDED): For %g, consider also the size of + the thousands separators. + 2025-04-10 Bruno Haible vasnprintf: Fix handling of # flag in %b, %B directives. diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index a0eab63f47..b195aa584c 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -2088,6 +2088,12 @@ MAX_ROOM_NEEDED (const arguments *ap, size_t arg_index, FCHAR_T conversion, tmp_length = xsum (tmp_length, 2); break; + case 'e': case 'E': + tmp_length = + 12; /* sign, decimal point, exponent etc. */ + tmp_length = xsum (tmp_length, precision); + break; + case 'f': case 'F': if (type == TYPE_LONGDOUBLE) tmp_length = @@ -2108,10 +2114,13 @@ MAX_ROOM_NEEDED (const arguments *ap, size_t arg_index, FCHAR_T conversion, tmp_length = xsum (tmp_length, precision); break; - case 'e': case 'E': case 'g': case 'G': + case 'g': case 'G': tmp_length = 12; /* sign, decimal point, exponent etc. */ - tmp_length = xsum (tmp_length, precision); + tmp_length = xsum (tmp_length, + precision + * 2 /* estimate for FLAG_GROUP */ + ); break; case 'a': case 'A':