From 304073ca60bde7a8c573ae83bd097459f6f629a0 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 18 Oct 2007 13:13:15 +0200 Subject: [PATCH] Improve detection of whether %n is supported or not. --- ChangeLog | 5 +++++ m4/printf.m4 | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 82d96c9cb1..c65dc07179 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2007-10-18 Bruno Haible + + * m4/printf.m4 (gl_PRINTF_DIRECTIVE_N, gl_SNPRINTF_DIRECTIVE_N): Put + the format string into writable memory. Needed in Fortify conditions. + 2007-10-18 Colin Watson (tiny change) Bruno Haible diff --git a/m4/printf.m4 b/m4/printf.m4 index 06ce939ab6..8de382b4ff 100644 --- a/m4/printf.m4 +++ b/m4/printf.m4 @@ -1,4 +1,4 @@ -# printf.m4 serial 16 +# printf.m4 serial 17 dnl Copyright (C) 2003, 2007 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -585,11 +585,16 @@ AC_DEFUN([gl_PRINTF_DIRECTIVE_N], AC_TRY_RUN([ #include #include +static char fmtstring[10]; static char buf[100]; int main () { int count = -1; - if (sprintf (buf, "%d %n", 123, &count, 33, 44, 55) < 0 + /* Copy the format string. Some systems (glibc with _FORTIFY_SOURCE=2) + support %n in format strings in read-only memory but not in writable + memory. */ + strcpy (fmtstring, "%d %n"); + if (sprintf (buf, fmtstring, 123, &count, 33, 44, 55) < 0 || strcmp (buf, "123 ") != 0 || count != 4) return 1; @@ -872,11 +877,16 @@ AC_DEFUN([gl_SNPRINTF_DIRECTIVE_N], AC_TRY_RUN([ #include #include +static char fmtstring[10]; static char buf[100]; int main () { int count = -1; - snprintf (buf, 4, "%d %n", 12345, &count, 33, 44, 55); + /* Copy the format string. Some systems (glibc with _FORTIFY_SOURCE=2) + support %n in format strings in read-only memory but not in writable + memory. */ + strcpy (fmtstring, "%d %n"); + snprintf (buf, 4, fmtstring, 12345, &count, 33, 44, 55); if (count != 6) return 1; return 0; -- 2.30.2