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=d0208009a7d8391109cde3baf7ae858ebd26443d;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 07de3b7b97..adad6ce81b 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-11 Bruno Haible newlocale: Support cross-compilation. diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index a07c1377dd..559b3c7043 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -2094,6 +2094,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 = @@ -2114,10 +2120,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':