Files
Paul Eggert 08c8a9228f vasnprintf: fix ("%*d", INT_MAX, n)
I ran into this problem when doing stress testing with GNU m4.
The problem is that ("%*d", INT_MAX, n) fails because the code
refuses to create a buffer of size INT_MAX + 1u,
due to problems when dealing with older nonconforming snprintf.
The simplest fix was to use snprintf only if it handles
sizes like INT_MAX + 1u, and to fall back on sprintf otherwise.
Nowadays I think snprintf should work on most practical targets.
Assuming this patch works out, perhaps we could simplify
lib/vasnprintf.c to ease further maintenance,
as nowadays I hope we can assume both HAVE_SNPRINTF_RETVAL_C99 and
HAVE_SNPRINTF_TRUNCATION_C99 on platforms where USE_SNPRINTF.
* lib/vasnprintf.c: Include minmax.h.
(USE_SNPRINTF): Define to 1 only if glibc 2+, Android, musl,
the BSDs, macOS, or Microsoft UCRT.
(VASNPRINTF) [USE_SNPRINTF]:
Allow maxlen to be 1 greater than INT_MAX on 64-bit platforms.
Document the limit’s derivation by using named locals.
Do not attempt to work around bugs in pre-C99 implementations,
as USE_SNPRINTF is pickier now.
* m4/vasnprintf.m4 (gl_PREREQ_VASNWPRINTF):
Move gl_MUSL_LIBC call from here ...
(gl_PREREQ_VASNXPRINTF): ... to here.
* modules/c-vasnprintf, modules/unistdio/u16-u16-vasnprintf:
* modules/unistdio/u16-vasnprintf:
* modules/unistdio/u32-u32-vasnprintf:
* modules/unistdio/u32-vasnprintf:
* modules/unistdio/u8-u8-vasnprintf:
* modules/unistdio/u8-vasnprintf, modules/unistdio/ulc-vasnprintf:
* modules/vasnprintf, modules/vasnwprintf:
(Files): Add m4/musl.m4 if it isn’t there already.
(Depends-on): Add minmax.
* tests/test-vasnprintf-posix.c:
* tests/test-vasprintf-posix.c:
* tests/test-vaszprintf-posix.c:
Include <limits.h> and <errno.h> if needed.
(test_function): Test for the bug, if RUN_EXPENSIVE_TESTS=yes
in the environment; this the coreutils tradition and I
didn’t see any Gnulib tradition so I just used it.
2026-07-06 20:41:43 -07:00

66 lines
817 B
Plaintext

Description:
vswprintf with automatic memory allocation and bounded output size.
Files:
lib/float+.h
lib/printf-args.h
lib/printf-args.c
lib/wprintf-parse.h
lib/wprintf-parse.c
lib/printf-parse.c
lib/vasnwprintf.h
lib/vasnwprintf.c
lib/vasnprintf.c
lib/asnwprintf.c
m4/wint_t.m4
m4/intmax_t.m4
m4/stdint_h.m4
m4/inttypes_h.m4
m4/vasnprintf.m4
m4/printf.m4
m4/mbrtowc.m4
m4/math_h.m4
m4/exponentd.m4
m4/musl.m4
Depends-on:
stdio-h
alloca-opt
attribute
float-h
free-posix
limits-h
stdint-h
xsize
errno-h
memchr
assert-h
wchar-h
localeconv
mbszero
mbrtowc
wmemcpy
wmemset
minmax
streq
configure.ac:
AC_REQUIRE([AC_C_RESTRICT])
gl_FUNC_VASNWPRINTF
Makefile.am:
lib_SOURCES += wprintf-parse.c vasnwprintf.c
lib_SOURCES += asnwprintf.c
Include:
"vasnwprintf.h"
Link:
$(MBRTOWC_LIB)
License:
LGPLv2+
Maintainer:
all