diff lib/vasnprintf.c @ 9361:40389da21b02

Don't use %n on glibc >= 2.3 systems.
author Bruno Haible <bruno@clisp.org>
date Fri, 19 Oct 2007 01:50:42 +0200 (2007-10-18)
parents 46dab2096f98
children 8b3c48038257
line wrap: on
line diff
--- a/lib/vasnprintf.c
+++ b/lib/vasnprintf.c
@@ -3385,9 +3385,21 @@
 #endif
 		  *fbp = dp->conversion;
 #if USE_SNPRINTF
+# if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))
 		fbp[1] = '%';
 		fbp[2] = 'n';
 		fbp[3] = '\0';
+# else
+		/* On glibc2 systems from glibc >= 2.3 - probably also older
+		   ones - we know that snprintf's returns value conforms to
+		   ISO C 99: the gl_SNPRINTF_DIRECTIVE_N test passes.
+		   Therefore we can avoid using %n in this situation.
+		   On glibc2 systems from 2004-10-18 or newer, the use of %n
+		   in format strings in writable memory may crash the program
+		   (if compiled with _FORTIFY_SOURCE=2), so we should avoid it
+		   in this situation.  */
+		fbp[1] = '\0';
+# endif
 #else
 		fbp[1] = '\0';
 #endif