From: Bruno Haible Date: Fri, 11 Apr 2025 14:59:24 +0000 (+0200) Subject: vasnprintf: Follow glibc's behaviour on glibc systems. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=aef99bf93993103c35e967020385138a672f0f8d;p=gnulib.git vasnprintf: Follow glibc's behaviour on glibc systems. * lib/vasnprintf.c (DCHAR_MBSNLEN): Define fallback. (VASNPRINTF): Use it on for width handling on glibc. * modules/vasnprintf (Depends-on): Add mbsnlen. * modules/c-vasnprintf (Depends-on): Likewise. --- diff --git a/ChangeLog b/ChangeLog index adad6ce81b..dedd3ca34e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2025-04-11 Bruno Haible + + vasnprintf: Follow glibc's behaviour on glibc systems. + * lib/vasnprintf.c (DCHAR_MBSNLEN): Define fallback. + (VASNPRINTF): Use it on for width handling on glibc. + * modules/vasnprintf (Depends-on): Add mbsnlen. + * modules/c-vasnprintf (Depends-on): Likewise. + 2025-04-11 Bruno Haible vasnprintf: Fix memory size bound for %g with grouping and precision. diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index 559b3c7043..f91300cbd1 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -183,6 +183,13 @@ # define TCHAR_T char # endif #endif +#ifndef DCHAR_MBSNLEN +# if WIDE_CHAR_VERSION +# define DCHAR_MBSNLEN wcsnlen +# else +# define DCHAR_MBSNLEN mbsnlen +# endif +#endif #if !WIDE_CHAR_VERSION || !DCHAR_IS_TCHAR /* TCHAR_T is char. */ /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'. @@ -7100,6 +7107,23 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, against the number of _characters_ of the converted value. */ w = DCHAR_MBSNLEN (result + length, count); +# elif __GLIBC__ >= 2 + /* glibc prefers to compare the width against the number + of characters as well, but only for numeric conversion + specifiers. See + + + */ + switch (dp->conversion) + { + case 'd': case 'i': case 'u': + case 'f': case 'F': case 'g': case 'G': + w = DCHAR_MBSNLEN (result + length, count); + break; + default: + w = count; + break; + } # else /* The width is compared against the number of _bytes_ of the converted value, says POSIX. */ diff --git a/modules/c-vasnprintf b/modules/c-vasnprintf index eebaf3aed8..aaa6011809 100644 --- a/modules/c-vasnprintf +++ b/modules/c-vasnprintf @@ -36,6 +36,7 @@ errno-h memchr multiarch mbszero +mbsnlen configure.ac: AC_REQUIRE([AC_C_RESTRICT]) diff --git a/modules/vasnprintf b/modules/vasnprintf index 96fb208891..aa12d79139 100644 --- a/modules/vasnprintf +++ b/modules/vasnprintf @@ -33,6 +33,7 @@ memchr assert-h wchar-h mbszero +mbsnlen configure.ac: AC_REQUIRE([AC_C_RESTRICT])