Yesterday I wrote:
> The way I propose to do it:
>   - Remove the support for the %n directives from all *printf* modules
>     by default.
>   - Add a new module 'printf-with-n-directive' that re-enables this support
>     in all these modules.
>   - Add a NEWS entry to notify the packages.
> 
> This way, most packages that use Gnulib *printf will be immune against
> possible CVEs in this area.

Done through this patch:


2024-02-21  Bruno Haible  <br...@clisp.org>

        *printf-posix: Disable support for the 'n' directive by default.
        * m4/printf-with-n-directive.m4: New file.
        * modules/printf-with-n-directive: New file.
        * m4/dprintf-posix.m4 (gl_FUNC_DPRINTF_IS_POSIX): If
        gl_PRINTF_SUPPORT_N_DIRECTIVE is not defined, ignore the value of the
        gl_cv_func_printf_directive_n variable.
        * m4/fprintf-posix.m4 (gl_FUNC_FPRINTF_IS_POSIX): Likewise.
        * m4/obstack-printf-posix.m4 (gl_FUNC_OBSTACK_PRINTF_IS_POSIX): 
Likewise.
        * m4/sprintf-posix.m4 (gl_FUNC_SPRINTF_IS_POSIX): Likewise.
        * m4/vasnprintf-posix.m4 (gl_FUNC_VASNPRINTF_IS_POSIX): Likewise.
        * m4/vasprintf-posix.m4 (gl_FUNC_VASPRINTF_IS_POSIX): Likewise.
        * m4/vdprintf-posix.m4 (gl_FUNC_VDPRINTF_IS_POSIX): Likewise.
        * m4/vfprintf-posix.m4 (gl_FUNC_VFPRINTF_IS_POSIX): Likewise.
        * m4/vsprintf-posix.m4 (gl_FUNC_VSPRINTF_IS_POSIX): Likewise.
        * m4/snprintf-posix.m4 (gl_FUNC_SNPRINTF_IS_POSIX): If
        gl_PRINTF_SUPPORT_N_DIRECTIVE is not defined, ignore the values of the
        gl_cv_func_printf_directive_n and gl_cv_func_snprintf_directive_n
        variables.
        * m4/vsnprintf-posix.m4 (gl_FUNC_VSNPRINTF_IS_POSIX): Likewise.
        * lib/printf-parse.c (PRINTF_PARSE): If NEED_PRINTF_WITH_N_DIRECTIVE is
        not defined, treat the 'n' directive like an unknown directive.
        * lib/vasnprintf.c (VASNPRINTF): If NEED_PRINTF_WITH_N_DIRECTIVE is not
        defined, disable the processing of the 'n' directive.
        * tests/test-snprintf-posix.h (test_function): If
        NEED_PRINTF_WITH_N_DIRECTIVE is not defined, skip the %n test.
        * tests/test-sprintf-posix.h (test_function): Likewise.
        * tests/test-vasnprintf-posix.c (test_function): Likewise.
        * tests/test-vasnwprintf-posix.c (test_function): Likewise.
        * tests/test-vasprintf-posix.c (test_function): Likewise.
        * doc/posix-functions/dprintf.texi: Mention that support for the 'n'
        directive is only enabled together with the module
        'printf-with-n-directive'.
        * doc/posix-functions/fprintf.texi: Likewise.
        * doc/posix-functions/printf.texi: Likewise.
        * doc/posix-functions/snprintf.texi: Likewise.
        * doc/posix-functions/sprintf.texi: Likewise.
        * doc/posix-functions/vdprintf.texi: Likewise.
        * doc/posix-functions/vfprintf.texi: Likewise.
        * doc/posix-functions/vprintf.texi: Likewise.
        * doc/posix-functions/vsnprintf.texi: Likewise.
        * doc/posix-functions/vsprintf.texi: Likewise.
        * doc/glibc-functions/obstack_printf.texi: Likewise.
        * doc/glibc-functions/obstack_vprintf.texi: Likewise.
        * NEWS: Mention the change.

diff --git a/NEWS b/NEWS
index ed9b2e6569..6caf3ad8e1 100644
--- a/NEWS
+++ b/NEWS
@@ -74,6 +74,11 @@ User visible incompatible changes
 
 Date        Modules         Changes
 
+2024-02-21  *printf-posix   These modules no longer support the 'n' directive
+                            by default.  In order to keep the 'n' directive
+                            enabled, you need to additionally request the
+                            module 'printf-with-n-directive'.
+
 2024-02-09  strftime        This module, deprecated on 2017-07-23, is removed.
                             Use module 'nstrftime' instead.
 
@@ -812,15 +817,15 @@ Date        Modules         Changes
 2009-06-25  fpurge          The include file is changed from "fpurge.h" to
                             <stdio.h>.
 
-2009-04-26  modules/uniconv/u8-conv-from-enc
-            modules/uniconv/u16-conv-from-enc
-            modules/uniconv/u32-conv-from-enc
+2009-04-26  uniconv/u8-conv-from-enc
+            uniconv/u16-conv-from-enc
+            uniconv/u32-conv-from-enc
                             The calling convention of the functions
                             u*_conv_from_encoding is changed.
 
-2009-04-26  modules/uniconv/u8-conv-to-enc
-            modules/uniconv/u16-conv-to-enc
-            modules/uniconv/u32-conv-to-enc
+2009-04-26  uniconv/u8-conv-to-enc
+            uniconv/u16-conv-to-enc
+            uniconv/u32-conv-to-enc
                             The calling convention of the functions
                             u*_conv_to_encoding is changed.
 
diff --git a/doc/glibc-functions/obstack_printf.texi 
b/doc/glibc-functions/obstack_printf.texi
index 6cb1ad0068..6b1a9e6e90 100644
--- a/doc/glibc-functions/obstack_printf.texi
+++ b/doc/glibc-functions/obstack_printf.texi
@@ -48,9 +48,6 @@
 NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, Solaris 9,
 Cygwin 1.5.x, mingw, MSVC 14.
 @item
-This function does not support the @samp{n} directive on some platforms:
-MSVC 14.
-@item
 This function does not support the @samp{ls} directive on some platforms:
 OpenBSD 4.0, IRIX 6.5, Cygwin 1.5.x, Haiku.
 @item
@@ -87,9 +84,6 @@
 @item
 This function can crash in out-of-memory conditions on some platforms:
 FreeBSD 14.0, NetBSD 5.0.
-@item
-This function does not fully support the @samp{n} directive on some platforms:
-HP-UX 11, mingw, MSVC 14.
 @end itemize
 
 Portability problems fixed by Gnulib module @code{obstack-printf-gnu}:
@@ -99,6 +93,17 @@
 glibc 2.34, FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2, macOS 12.5, AIX 7.2, 
Solaris 11.4, and others.
 @end itemize
 
+Portability problems fixed by either Gnulib module @code{obstack-printf-posix} 
or @code{obstack-printf-gnu}
+and additionally the Gnulib module @code{printf-with-n-directive}:
+@itemize
+@item
+This function does not support the @samp{n} directive on some platforms:
+MSVC 14.
+@item
+This function does not fully support the @samp{n} directive on some platforms:
+HP-UX 11, mingw, MSVC 14.
+@end itemize
+
 Portability problems not fixed by Gnulib:
 @itemize
 @item
diff --git a/doc/glibc-functions/obstack_vprintf.texi 
b/doc/glibc-functions/obstack_vprintf.texi
index eed1b0ef6d..ad57a90f00 100644
--- a/doc/glibc-functions/obstack_vprintf.texi
+++ b/doc/glibc-functions/obstack_vprintf.texi
@@ -48,9 +48,6 @@
 NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, Solaris 9,
 Cygwin 1.5.x, mingw, MSVC 14.
 @item
-This function does not support the @samp{n} directive on some platforms:
-MSVC 14.
-@item
 This function does not support the @samp{ls} directive on some platforms:
 OpenBSD 4.0, IRIX 6.5, Cygwin 1.5.x, Haiku.
 @item
@@ -87,9 +84,6 @@
 @item
 This function can crash in out-of-memory conditions on some platforms:
 FreeBSD 14.0, NetBSD 5.0.
-@item
-This function does not fully support the @samp{n} directive on some platforms:
-HP-UX 11, mingw, MSVC 14.
 @end itemize
 
 Portability problems fixed by Gnulib module @code{obstack-printf-gnu}:
@@ -99,6 +93,17 @@
 glibc 2.34, FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2, macOS 12.5, AIX 7.2, 
Solaris 11.4, and others.
 @end itemize
 
+Portability problems fixed by either Gnulib module @code{obstack-printf-posix} 
or @code{obstack-printf-gnu}
+and additionally the Gnulib module @code{printf-with-n-directive}:
+@itemize
+@item
+This function does not support the @samp{n} directive on some platforms:
+MSVC 14.
+@item
+This function does not fully support the @samp{n} directive on some platforms:
+HP-UX 11, mingw, MSVC 14.
+@end itemize
+
 Portability problems not fixed by Gnulib:
 @itemize
 @item
diff --git a/doc/posix-functions/dprintf.texi b/doc/posix-functions/dprintf.texi
index 4a30b77663..4b2714ced5 100644
--- a/doc/posix-functions/dprintf.texi
+++ b/doc/posix-functions/dprintf.texi
@@ -35,10 +35,6 @@
 glibc 2.34, musl libc, macOS 12.5, FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2,
 AIX 7.2, Solaris 11.4, Cygwin 2.9.0.
 @item
-This function does not support the @samp{n} directive on some platforms:
-glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
-Android, OpenBSD, macOS 11.1.
-@item
 This function does not support precisions in the @samp{ls} directive correctly
 on some platforms:
 Solaris 11.4.
@@ -67,6 +63,15 @@
 glibc 2.34, FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2, macOS 12.5, AIX 7.2, 
Solaris 11.4, and others.
 @end itemize
 
+Portability problems fixed by either Gnulib module @code{dprintf-posix} or 
@code{dprintf-gnu}
+and additionally the Gnulib module @code{printf-with-n-directive}:
+@itemize
+@item
+This function does not support the @samp{n} directive on some platforms:
+glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
+Android, OpenBSD, macOS 11.1.
+@end itemize
+
 Portability problems not fixed by Gnulib:
 @itemize
 @item
diff --git a/doc/posix-functions/fprintf.texi b/doc/posix-functions/fprintf.texi
index 40d1aff463..421e88b1d1 100644
--- a/doc/posix-functions/fprintf.texi
+++ b/doc/posix-functions/fprintf.texi
@@ -40,10 +40,6 @@
 NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, Solaris 9,
 Cygwin 1.5.x, mingw, MSVC 14.
 @item
-This function does not support the @samp{n} directive on some platforms:
-glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
-Android, OpenBSD, macOS 11.1, MSVC 14.
-@item
 This function does not support the @samp{ls} directive on some platforms:
 OpenBSD 4.0, IRIX 6.5, Cygwin 1.5.x, Haiku.
 @item
@@ -94,6 +90,15 @@
 glibc 2.34, FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2, macOS 12.5, AIX 7.2, 
Solaris 11.4, and others.
 @end itemize
 
+Portability problems fixed by either Gnulib module @code{fprintf-posix} or 
@code{fprintf-gnu}
+and additionally the Gnulib module @code{printf-with-n-directive}:
+@itemize
+@item
+This function does not support the @samp{n} directive on some platforms:
+glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
+Android, OpenBSD, macOS 11.1, MSVC 14.
+@end itemize
+
 Portability problems fixed by Gnulib module @code{stdio} or 
@code{fprintf-posix} or @code{fprintf-gnu}, together with module 
@code{nonblocking}:
 @itemize
 @item
diff --git a/doc/posix-functions/printf.texi b/doc/posix-functions/printf.texi
index 4b34314630..f6cc6f2f67 100644
--- a/doc/posix-functions/printf.texi
+++ b/doc/posix-functions/printf.texi
@@ -40,10 +40,6 @@
 NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, Solaris 9,
 Cygwin 1.5.x, mingw, MSVC 14.
 @item
-This function does not support the @samp{n} directive on some platforms:
-glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
-Android, OpenBSD, macOS 11.1, MSVC 14.
-@item
 This function does not support the @samp{ls} directive on some platforms:
 OpenBSD 4.0, IRIX 6.5, Cygwin 1.5.x, Haiku.
 @item
@@ -94,6 +90,15 @@
 glibc 2.34, FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2, macOS 12.5, AIX 7.2, 
Solaris 11.4, and others.
 @end itemize
 
+Portability problems fixed by either Gnulib module @code{printf-posix} or 
@code{printf-gnu}
+and additionally the Gnulib module @code{printf-with-n-directive}:
+@itemize
+@item
+This function does not support the @samp{n} directive on some platforms:
+glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
+Android, OpenBSD, macOS 11.1, MSVC 14.
+@end itemize
+
 Portability problems fixed by Gnulib module @code{stdio} or 
@code{printf-posix} or @code{printf-gnu}, together with module 
@code{nonblocking}:
 @itemize
 @item
diff --git a/doc/posix-functions/snprintf.texi 
b/doc/posix-functions/snprintf.texi
index 582951e55d..61267260ac 100644
--- a/doc/posix-functions/snprintf.texi
+++ b/doc/posix-functions/snprintf.texi
@@ -55,10 +55,6 @@
 NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, Solaris 9,
 Cygwin 1.5.x, mingw, MSVC 14.
 @item
-This function does not support the @samp{n} directive on some platforms:
-glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
-Android, OpenBSD, macOS 11.1, MSVC 14.
-@item
 This function does not support the @samp{ls} directive on some platforms:
 OpenBSD 4.0, IRIX 6.5, Cygwin 1.5.x, Haiku.
 @item
@@ -111,6 +107,15 @@
 glibc 2.34, FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2, macOS 12.5, AIX 7.2, 
Solaris 11.4, and others.
 @end itemize
 
+Portability problems fixed by either Gnulib module @code{snprintf-posix} or 
@code{snprintf-gnu}
+and additionally the Gnulib module @code{printf-with-n-directive}:
+@itemize
+@item
+This function does not support the @samp{n} directive on some platforms:
+glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
+Android, OpenBSD, macOS 11.1, MSVC 14.
+@end itemize
+
 Portability problems not fixed by Gnulib:
 @itemize
 @item
diff --git a/doc/posix-functions/sprintf.texi b/doc/posix-functions/sprintf.texi
index d896778583..b8a0b6fc1a 100644
--- a/doc/posix-functions/sprintf.texi
+++ b/doc/posix-functions/sprintf.texi
@@ -40,10 +40,6 @@
 NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, Solaris 9,
 Cygwin 1.5.x, mingw, MSVC 14.
 @item
-This function does not support the @samp{n} directive on some platforms:
-glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
-Android, OpenBSD, macOS 11.1, MSVC 14.
-@item
 This function does not support the @samp{ls} directive on some platforms:
 OpenBSD 4.0, IRIX 6.5, Cygwin 1.5.x, Haiku.
 @item
@@ -97,6 +93,15 @@
 glibc 2.34, FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2, macOS 12.5, AIX 7.2, 
Solaris 11.4, and others.
 @end itemize
 
+Portability problems fixed by either Gnulib module @code{sprintf-posix} or 
@code{sprintf-gnu}
+and additionally the Gnulib module @code{printf-with-n-directive}:
+@itemize
+@item
+This function does not support the @samp{n} directive on some platforms:
+glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
+Android, OpenBSD, macOS 11.1, MSVC 14.
+@end itemize
+
 Portability problems not fixed by Gnulib:
 @itemize
 @item
diff --git a/doc/posix-functions/vdprintf.texi 
b/doc/posix-functions/vdprintf.texi
index 8ce6b859f4..f3b3d3f0f8 100644
--- a/doc/posix-functions/vdprintf.texi
+++ b/doc/posix-functions/vdprintf.texi
@@ -35,10 +35,6 @@
 glibc 2.34, musl libc, macOS 12.5, FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2,
 AIX 7.2, Solaris 11.4, Cygwin 2.9.0.
 @item
-This function does not support the @samp{n} directive on some platforms:
-glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
-Android, OpenBSD, macOS 11.1.
-@item
 This function does not support precisions in the @samp{ls} directive correctly
 on some platforms:
 Solaris 11.4.
@@ -67,6 +63,15 @@
 glibc 2.34, FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2, macOS 12.5, AIX 7.2, 
Solaris 11.4, and others.
 @end itemize
 
+Portability problems fixed by either Gnulib module @code{vdprintf-posix} or 
@code{vdprintf-gnu}
+and additionally the Gnulib module @code{printf-with-n-directive}:
+@itemize
+@item
+This function does not support the @samp{n} directive on some platforms:
+glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
+Android, OpenBSD, macOS 11.1.
+@end itemize
+
 Portability problems not fixed by Gnulib:
 @itemize
 @item
diff --git a/doc/posix-functions/vfprintf.texi 
b/doc/posix-functions/vfprintf.texi
index df40d348ec..bfbb39b29f 100644
--- a/doc/posix-functions/vfprintf.texi
+++ b/doc/posix-functions/vfprintf.texi
@@ -40,10 +40,6 @@
 NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, Solaris 9,
 Cygwin 1.5.x, mingw, MSVC 14.
 @item
-This function does not support the @samp{n} directive on some platforms:
-glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
-Android, OpenBSD, macOS 11.1, MSVC 14.
-@item
 This function does not support the @samp{ls} directive on some platforms:
 OpenBSD 4.0, IRIX 6.5, Cygwin 1.5.x, Haiku.
 @item
@@ -94,6 +90,15 @@
 glibc 2.34, FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2, macOS 12.5, AIX 7.2, 
Solaris 11.4, and others.
 @end itemize
 
+Portability problems fixed by either Gnulib module @code{vfprintf-posix} or 
@code{vfprintf-gnu}
+and additionally the Gnulib module @code{printf-with-n-directive}:
+@itemize
+@item
+This function does not support the @samp{n} directive on some platforms:
+glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
+Android, OpenBSD, macOS 11.1, MSVC 14.
+@end itemize
+
 Portability problems fixed by Gnulib module @code{stdio} or 
@code{vfprintf-posix} or @code{vfprintf-gnu}, together with module 
@code{nonblocking}:
 @itemize
 @item
diff --git a/doc/posix-functions/vprintf.texi b/doc/posix-functions/vprintf.texi
index 2c32262450..c30dc2ea15 100644
--- a/doc/posix-functions/vprintf.texi
+++ b/doc/posix-functions/vprintf.texi
@@ -40,10 +40,6 @@
 NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, Solaris 9,
 Cygwin 1.5.x, mingw, MSVC 14.
 @item
-This function does not support the @samp{n} directive on some platforms:
-glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
-Android, OpenBSD, macOS 11.1, MSVC 14.
-@item
 This function does not support the @samp{ls} directive on some platforms:
 OpenBSD 4.0, IRIX 6.5, Cygwin 1.5.x, Haiku.
 @item
@@ -94,6 +90,15 @@
 glibc 2.34, FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2, macOS 12.5, AIX 7.2, 
Solaris 11.4, and others.
 @end itemize
 
+Portability problems fixed by either Gnulib module @code{vprintf-posix} or 
@code{vprintf-gnu}
+and additionally the Gnulib module @code{printf-with-n-directive}:
+@itemize
+@item
+This function does not support the @samp{n} directive on some platforms:
+glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
+Android, OpenBSD, macOS 11.1, MSVC 14.
+@end itemize
+
 Portability problems fixed by Gnulib module @code{stdio} or 
@code{vprintf-posix} or @code{vprintf-gnu}, together with module 
@code{nonblocking}:
 @itemize
 @item
diff --git a/doc/posix-functions/vsnprintf.texi 
b/doc/posix-functions/vsnprintf.texi
index d6005e60aa..b2613c9183 100644
--- a/doc/posix-functions/vsnprintf.texi
+++ b/doc/posix-functions/vsnprintf.texi
@@ -93,10 +93,6 @@
 This function does not truncate the result as specified in C99 on some 
platforms:
 mingw, MSVC 14.
 @item
-This function does not fully support the @samp{n} directive on some platforms:
-glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
-Android, OpenBSD, macOS 11.1, HP-UX 11, mingw, MSVC 14.
-@item
 This function overwrites memory even when a zero size argument is passed on 
some
 platforms:
 HP-UX 11.
@@ -109,6 +105,15 @@
 glibc 2.34, FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2, macOS 12.5, AIX 7.2, 
Solaris 11.4, and others.
 @end itemize
 
+Portability problems fixed by either Gnulib module @code{vsnprintf-posix} or 
@code{vsnprintf-gnu}
+and additionally the Gnulib module @code{printf-with-n-directive}:
+@itemize
+@item
+This function does not fully support the @samp{n} directive on some platforms:
+glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
+Android, OpenBSD, macOS 11.1, HP-UX 11, mingw, MSVC 14.
+@end itemize
+
 Portability problems not fixed by Gnulib:
 @itemize
 @item
diff --git a/doc/posix-functions/vsprintf.texi 
b/doc/posix-functions/vsprintf.texi
index 79058f53f4..328e8b9e96 100644
--- a/doc/posix-functions/vsprintf.texi
+++ b/doc/posix-functions/vsprintf.texi
@@ -40,10 +40,6 @@
 NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, Solaris 9,
 Cygwin 1.5.x, mingw, MSVC 14.
 @item
-This function does not support the @samp{n} directive on some platforms:
-glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
-Android, OpenBSD, macOS 11.1, MSVC 14.
-@item
 This function does not support the @samp{ls} directive on some platforms:
 OpenBSD 4.0, IRIX 6.5, Cygwin 1.5.x, Haiku.
 @item
@@ -97,6 +93,15 @@
 glibc 2.34, FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2, macOS 12.5, AIX 7.2, 
Solaris 11.4, and others.
 @end itemize
 
+Portability problems fixed by either Gnulib module @code{vsprintf-posix} or 
@code{vsprintf-gnu}
+and additionally the Gnulib module @code{printf-with-n-directive}:
+@itemize
+@item
+This function does not support the @samp{n} directive on some platforms:
+glibc when used with @code{_FORTIFY_SOURCE >= 2} (set by default on Ubuntu),
+Android, OpenBSD, macOS 11.1, MSVC 14.
+@end itemize
+
 Portability problems not fixed by Gnulib:
 @itemize
 @item
diff --git a/lib/printf-parse.c b/lib/printf-parse.c
index c0532900ad..a33e27a0b8 100644
--- a/lib/printf-parse.c
+++ b/lib/printf-parse.c
@@ -558,6 +558,7 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, 
arguments *a)
                 cp += 3;
               }
 #endif
+            (void) pointer_type;
 
             /* Read the conversion character.  */
             c = *cp++;
@@ -616,9 +617,11 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, 
arguments *a)
               case 'p':
                 type = TYPE_POINTER;
                 break;
+#if NEED_PRINTF_WITH_N_DIRECTIVE
               case 'n':
                 type = pointer_type;
                 break;
+#endif
 #if ENABLE_UNISTDIO
               /* The unistdio extensions.  */
               case 'U':
diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c
index 544ae62d9f..de20445894 100644
--- a/lib/vasnprintf.c
+++ b/lib/vasnprintf.c
@@ -2335,6 +2335,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
 
             if (dp->conversion == 'n')
               {
+#if NEED_PRINTF_WITH_N_DIRECTIVE
                 switch (a.arg[dp->arg_index].type)
                   {
                   case TYPE_COUNT_SCHAR_POINTER:
@@ -2379,6 +2380,9 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
                   default:
                     abort ();
                   }
+#else
+                abort ();
+#endif
               }
 #if ENABLE_UNISTDIO
             /* The unistdio extensions.  */
diff --git a/m4/dprintf-posix.m4 b/m4/dprintf-posix.m4
index bfdf155290..56189ca551 100644
--- a/m4/dprintf-posix.m4
+++ b/m4/dprintf-posix.m4
@@ -1,4 +1,4 @@
-# dprintf-posix.m4 serial 8
+# dprintf-posix.m4 serial 9
 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -55,7 +55,7 @@ AC_DEFUN([gl_FUNC_DPRINTF_IS_POSIX]
                               *yes)
                                 case "$gl_cv_func_printf_directive_f" in
                                   *yes)
-                                    case "$gl_cv_func_printf_directive_n" in
+                                    case 
m4_ifdef([gl_PRINTF_SUPPORT_N_DIRECTIVE],["$gl_cv_func_printf_directive_n"],["yes"])
 in
                                       *yes)
                                         case "$gl_cv_func_printf_directive_ls" 
in
                                           *yes)
diff --git a/m4/fprintf-posix.m4 b/m4/fprintf-posix.m4
index 97af45f692..c23e10f953 100644
--- a/m4/fprintf-posix.m4
+++ b/m4/fprintf-posix.m4
@@ -1,4 +1,4 @@
-# fprintf-posix.m4 serial 19
+# fprintf-posix.m4 serial 20
 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -53,7 +53,7 @@ AC_DEFUN([gl_FUNC_FPRINTF_IS_POSIX]
                             *yes)
                               case "$gl_cv_func_printf_directive_f" in
                                 *yes)
-                                  case "$gl_cv_func_printf_directive_n" in
+                                  case 
m4_ifdef([gl_PRINTF_SUPPORT_N_DIRECTIVE],["$gl_cv_func_printf_directive_n"],["yes"])
 in
                                     *yes)
                                       case "$gl_cv_func_printf_directive_ls" in
                                         *yes)
diff --git a/m4/obstack-printf-posix.m4 b/m4/obstack-printf-posix.m4
index 3c5c377263..e1fc538176 100644
--- a/m4/obstack-printf-posix.m4
+++ b/m4/obstack-printf-posix.m4
@@ -1,4 +1,4 @@
-# obstack-printf-posix.m4 serial 9
+# obstack-printf-posix.m4 serial 10
 dnl Copyright (C) 2008-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -59,7 +59,7 @@ AC_DEFUN([gl_FUNC_OBSTACK_PRINTF_IS_POSIX]
                               *yes)
                                 case "$gl_cv_func_printf_directive_f" in
                                   *yes)
-                                    case "$gl_cv_func_printf_directive_n" in
+                                    case 
m4_ifdef([gl_PRINTF_SUPPORT_N_DIRECTIVE],["$gl_cv_func_printf_directive_n"],["yes"])
 in
                                       *yes)
                                         case "$gl_cv_func_printf_directive_ls" 
in
                                           *yes)
diff --git a/m4/printf-with-n-directive.m4 b/m4/printf-with-n-directive.m4
new file mode 100644
index 0000000000..b3b7fcb5b3
--- /dev/null
+++ b/m4/printf-with-n-directive.m4
@@ -0,0 +1,12 @@
+# printf-with-n-directive.m4 serial 1
+dnl Copyright (C) 2024 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+dnl This macro is a witness that support of the %n directive in *printf
+dnl functions is desired.
+AC_DEFUN([gl_PRINTF_SUPPORT_N_DIRECTIVE], [
+  AC_DEFINE([NEED_PRINTF_WITH_N_DIRECTIVE], [1],
+    [Define to 1 to enable support of the %n directive in *printf functions.])
+])
diff --git a/m4/snprintf-posix.m4 b/m4/snprintf-posix.m4
index 01c69cfbec..563f6aa8f7 100644
--- a/m4/snprintf-posix.m4
+++ b/m4/snprintf-posix.m4
@@ -1,4 +1,4 @@
-# snprintf-posix.m4 serial 19
+# snprintf-posix.m4 serial 21
 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -60,7 +60,7 @@ AC_DEFUN([gl_FUNC_SNPRINTF_IS_POSIX]
                               *yes)
                                 case "$gl_cv_func_printf_directive_f" in
                                   *yes)
-                                    case "$gl_cv_func_printf_directive_n" in
+                                    case 
m4_ifdef([gl_PRINTF_SUPPORT_N_DIRECTIVE],["$gl_cv_func_printf_directive_n"],["yes"])
 in
                                       *yes)
                                         case "$gl_cv_func_printf_directive_ls" 
in
                                           *yes)
@@ -84,7 +84,7 @@ AC_DEFUN([gl_FUNC_SNPRINTF_IS_POSIX]
                                                                               
*yes)
                                                                                
 case "$gl_cv_func_snprintf_retval_c99" in
                                                                                
   *yes)
-                                                                               
     case "$gl_cv_func_snprintf_directive_n" in
+                                                                               
     case 
m4_ifdef([gl_PRINTF_SUPPORT_N_DIRECTIVE],["$gl_cv_func_snprintf_directive_n"],["yes"])
 in
                                                                                
       *yes)
                                                                                
         case "$gl_cv_func_snprintf_size1" in
                                                                                
           *yes)
diff --git a/m4/sprintf-posix.m4 b/m4/sprintf-posix.m4
index 09f28ded9e..c7ce64a1bf 100644
--- a/m4/sprintf-posix.m4
+++ b/m4/sprintf-posix.m4
@@ -1,4 +1,4 @@
-# sprintf-posix.m4 serial 17
+# sprintf-posix.m4 serial 18
 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -53,7 +53,7 @@ AC_DEFUN([gl_FUNC_SPRINTF_IS_POSIX]
                             *yes)
                               case "$gl_cv_func_printf_directive_f" in
                                 *yes)
-                                  case "$gl_cv_func_printf_directive_n" in
+                                  case 
m4_ifdef([gl_PRINTF_SUPPORT_N_DIRECTIVE],["$gl_cv_func_printf_directive_n"],["yes"])
 in
                                     *yes)
                                       case "$gl_cv_func_printf_directive_ls" in
                                         *yes)
diff --git a/m4/vasnprintf-posix.m4 b/m4/vasnprintf-posix.m4
index a3bd402a81..93ae256f29 100644
--- a/m4/vasnprintf-posix.m4
+++ b/m4/vasnprintf-posix.m4
@@ -1,4 +1,4 @@
-# vasnprintf-posix.m4 serial 18
+# vasnprintf-posix.m4 serial 19
 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -53,7 +53,7 @@ AC_DEFUN_ONCE([gl_FUNC_VASNPRINTF_IS_POSIX]
                             *yes)
                               case "$gl_cv_func_printf_directive_f" in
                                 *yes)
-                                  case "$gl_cv_func_printf_directive_n" in
+                                  case 
m4_ifdef([gl_PRINTF_SUPPORT_N_DIRECTIVE],["$gl_cv_func_printf_directive_n"],["yes"])
 in
                                     *yes)
                                       case "$gl_cv_func_printf_directive_ls" in
                                         *yes)
diff --git a/m4/vasprintf-posix.m4 b/m4/vasprintf-posix.m4
index b82ccd3df7..02537f2d48 100644
--- a/m4/vasprintf-posix.m4
+++ b/m4/vasprintf-posix.m4
@@ -1,4 +1,4 @@
-# vasprintf-posix.m4 serial 18
+# vasprintf-posix.m4 serial 19
 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -54,7 +54,7 @@ AC_DEFUN([gl_FUNC_VASPRINTF_IS_POSIX]
                             *yes)
                               case "$gl_cv_func_printf_directive_f" in
                                 *yes)
-                                  case "$gl_cv_func_printf_directive_n" in
+                                  case 
m4_ifdef([gl_PRINTF_SUPPORT_N_DIRECTIVE],["$gl_cv_func_printf_directive_n"],["yes"])
 in
                                     *yes)
                                       case "$gl_cv_func_printf_directive_ls" in
                                         *yes)
diff --git a/m4/vdprintf-posix.m4 b/m4/vdprintf-posix.m4
index d3a9f129ba..84ebbabc52 100644
--- a/m4/vdprintf-posix.m4
+++ b/m4/vdprintf-posix.m4
@@ -1,4 +1,4 @@
-# vdprintf-posix.m4 serial 8
+# vdprintf-posix.m4 serial 9
 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -55,7 +55,7 @@ AC_DEFUN([gl_FUNC_VDPRINTF_IS_POSIX]
                               *yes)
                                 case "$gl_cv_func_printf_directive_f" in
                                   *yes)
-                                    case "$gl_cv_func_printf_directive_n" in
+                                    case 
m4_ifdef([gl_PRINTF_SUPPORT_N_DIRECTIVE],["$gl_cv_func_printf_directive_n"],["yes"])
 in
                                       *yes)
                                         case "$gl_cv_func_printf_directive_ls" 
in
                                           *yes)
diff --git a/m4/vfprintf-posix.m4 b/m4/vfprintf-posix.m4
index 9c53db9d42..5c53a8b215 100644
--- a/m4/vfprintf-posix.m4
+++ b/m4/vfprintf-posix.m4
@@ -1,4 +1,4 @@
-# vfprintf-posix.m4 serial 19
+# vfprintf-posix.m4 serial 20
 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -53,7 +53,7 @@ AC_DEFUN([gl_FUNC_VFPRINTF_IS_POSIX]
                             *yes)
                               case "$gl_cv_func_printf_directive_f" in
                                 *yes)
-                                  case "$gl_cv_func_printf_directive_n" in
+                                  case 
m4_ifdef([gl_PRINTF_SUPPORT_N_DIRECTIVE],["$gl_cv_func_printf_directive_n"],["yes"])
 in
                                     *yes)
                                       case "$gl_cv_func_printf_directive_ls" in
                                         *yes)
diff --git a/m4/vsnprintf-posix.m4 b/m4/vsnprintf-posix.m4
index 94d3d18ef9..cec5b1af85 100644
--- a/m4/vsnprintf-posix.m4
+++ b/m4/vsnprintf-posix.m4
@@ -1,4 +1,4 @@
-# vsnprintf-posix.m4 serial 20
+# vsnprintf-posix.m4 serial 22
 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -61,7 +61,7 @@ AC_DEFUN([gl_FUNC_VSNPRINTF_IS_POSIX]
                               *yes)
                                 case "$gl_cv_func_printf_directive_f" in
                                   *yes)
-                                    case "$gl_cv_func_printf_directive_n" in
+                                    case 
m4_ifdef([gl_PRINTF_SUPPORT_N_DIRECTIVE],["$gl_cv_func_printf_directive_n"],["yes"])
 in
                                       *yes)
                                         case "$gl_cv_func_printf_directive_ls" 
in
                                           *yes)
@@ -85,7 +85,7 @@ AC_DEFUN([gl_FUNC_VSNPRINTF_IS_POSIX]
                                                                               
*yes)
                                                                                
 case "$gl_cv_func_snprintf_retval_c99" in
                                                                                
   *yes)
-                                                                               
     case "$gl_cv_func_snprintf_directive_n" in
+                                                                               
     case 
m4_ifdef([gl_PRINTF_SUPPORT_N_DIRECTIVE],["$gl_cv_func_snprintf_directive_n"],["yes"])
 in
                                                                                
       *yes)
                                                                                
         case "$gl_cv_func_snprintf_size1" in
                                                                                
           *yes)
diff --git a/m4/vsprintf-posix.m4 b/m4/vsprintf-posix.m4
index b99ab10153..71ea4adb20 100644
--- a/m4/vsprintf-posix.m4
+++ b/m4/vsprintf-posix.m4
@@ -1,4 +1,4 @@
-# vsprintf-posix.m4 serial 17
+# vsprintf-posix.m4 serial 18
 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -53,7 +53,7 @@ AC_DEFUN([gl_FUNC_VSPRINTF_IS_POSIX]
                             *yes)
                               case "$gl_cv_func_printf_directive_f" in
                                 *yes)
-                                  case "$gl_cv_func_printf_directive_n" in
+                                  case 
m4_ifdef([gl_PRINTF_SUPPORT_N_DIRECTIVE],["$gl_cv_func_printf_directive_n"],["yes"])
 in
                                     *yes)
                                       case "$gl_cv_func_printf_directive_ls" in
                                         *yes)
diff --git a/modules/printf-with-n-directive b/modules/printf-with-n-directive
new file mode 100644
index 0000000000..6a99c94b47
--- /dev/null
+++ b/modules/printf-with-n-directive
@@ -0,0 +1,26 @@
+Description:
+Enable processing of the %n directive in *printf functions.
+
+Comment:
+This module should not be used as a dependency from a test module,
+otherwise when this module occurs as a tests-related module, it will
+have side effects on the compilation of the 'vasnprintf' module, if
+that module occurs among the main modules in lib/.
+
+Files:
+m4/printf-with-n-directive.m4
+
+Depends-on:
+
+configure.ac:
+gl_PRINTF_SUPPORT_N_DIRECTIVE
+
+Makefile.am:
+
+Include:
+
+License:
+LGPLv2+
+
+Maintainer:
+all
diff --git a/tests/test-snprintf-posix.h b/tests/test-snprintf-posix.h
index 99c2f8d738..40c45db1bb 100644
--- a/tests/test-snprintf-posix.h
+++ b/tests/test-snprintf-posix.h
@@ -2822,6 +2822,7 @@ test_function (int (*my_snprintf) (char *, size_t, const 
char *, ...))
     ASSERT (retval == strlen (result));
   }
 
+#if NEED_PRINTF_WITH_N_DIRECTIVE
   /* Test the support of the %n format directive.  */
 
   {
@@ -2832,6 +2833,7 @@ test_function (int (*my_snprintf) (char *, size_t, const 
char *, ...))
     ASSERT (retval == strlen (result));
     ASSERT (count == 4);
   }
+#endif
 
   /* Test the support of the POSIX/XSI format strings with positions.  */
 
diff --git a/tests/test-sprintf-posix.h b/tests/test-sprintf-posix.h
index 9a0fcedae4..f09a17fd48 100644
--- a/tests/test-sprintf-posix.h
+++ b/tests/test-sprintf-posix.h
@@ -2808,6 +2808,7 @@ test_function (int (*my_sprintf) (char *, const char *, 
...))
     ASSERT (retval == strlen (result));
   }
 
+#if NEED_PRINTF_WITH_N_DIRECTIVE
   /* Test the support of the %n format directive.  */
 
   {
@@ -2818,6 +2819,7 @@ test_function (int (*my_sprintf) (char *, const char *, 
...))
     ASSERT (retval == strlen (result));
     ASSERT (count == 4);
   }
+#endif
 
   /* Test the support of the POSIX/XSI format strings with positions.  */
 
diff --git a/tests/test-vasnprintf-posix.c b/tests/test-vasnprintf-posix.c
index 5c14b88dd1..26a9733284 100644
--- a/tests/test-vasnprintf-posix.c
+++ b/tests/test-vasnprintf-posix.c
@@ -3630,6 +3630,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, 
const char *, ...))
     free (result);
   }
 
+#if NEED_PRINTF_WITH_N_DIRECTIVE
   /* Test the support of the %n format directive.  */
 
   {
@@ -3643,6 +3644,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, 
const char *, ...))
     ASSERT (count == 4);
     free (result);
   }
+#endif
 
   /* Test the support of the POSIX/XSI format strings with positions.  */
 
diff --git a/tests/test-vasnwprintf-posix.c b/tests/test-vasnwprintf-posix.c
index af0bb64bbb..fb6ce0fdd8 100644
--- a/tests/test-vasnwprintf-posix.c
+++ b/tests/test-vasnwprintf-posix.c
@@ -3630,6 +3630,7 @@ test_function (wchar_t * (*my_asnwprintf) (wchar_t *, 
size_t *, const wchar_t *,
     free (result);
   }
 
+#if NEED_PRINTF_WITH_N_DIRECTIVE
   /* Test the support of the %n format directive.  */
 
   {
@@ -3643,6 +3644,7 @@ test_function (wchar_t * (*my_asnwprintf) (wchar_t *, 
size_t *, const wchar_t *,
     ASSERT (count == 4);
     free (result);
   }
+#endif
 
   /* Test the support of the POSIX/XSI format strings with positions.  */
 
diff --git a/tests/test-vasprintf-posix.c b/tests/test-vasprintf-posix.c
index bcd1077dd9..f6f9159ddd 100644
--- a/tests/test-vasprintf-posix.c
+++ b/tests/test-vasprintf-posix.c
@@ -3615,6 +3615,7 @@ test_function (int (*my_asprintf) (char **, const char *, 
...))
     free (result);
   }
 
+#if NEED_PRINTF_WITH_N_DIRECTIVE
   /* Test the support of the %n format directive.  */
 
   {
@@ -3628,6 +3629,7 @@ test_function (int (*my_asprintf) (char **, const char *, 
...))
     ASSERT (count == 4);
     free (result);
   }
+#endif
 
   /* Test the support of the POSIX/XSI format strings with positions.  */
 




Reply via email to