Support -Wformat -Werror=format-security.

Check if gcc supports -Wformat -Werror=format-security and use it if so.
Fix m_gdbserver/remote-utils.c sr_perror call. Fixes Bug #334727.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@13962 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/Makefile.tool-tests.am b/Makefile.tool-tests.am
index 6521afb..baefbd7 100644
--- a/Makefile.tool-tests.am
+++ b/Makefile.tool-tests.am
@@ -32,6 +32,10 @@
 CFLAGS += -Wno-write-strings
 endif
 
+if HAS_ERROR_FORMAT_SECURITY
+CFLAGS += -Wformat -Werror=format-security
+endif
+
 if COMPILER_IS_CLANG
 CFLAGS   += -Wno-format-extra-args       # perf/tinycc.c
 CFLAGS   += -Wno-literal-range           # none/tests/amd64/fxtract.c
diff --git a/NEWS b/NEWS
index e8f3b86..2a3840c 100644
--- a/NEWS
+++ b/NEWS
@@ -119,6 +119,7 @@
 333228  AAarch64 Missing instruction encoding: mrs %[reg], ctr_el0
 333230  AAarch64 missing instruction encodings: dc, ic, dsb.
 333666  Recognize MPX instructions and bnd prefix.
+334727  Build fails with -Werror=format-security
 n-i-bz  Fix KVM_CREATE_IRQCHIP ioctl handling
 n-i-bz  s390x: Fix memory corruption for multithreaded applications
 n-i-bz  vex arm->IR: allow PC as basereg in some LDRD cases
diff --git a/configure.ac b/configure.ac
index af42289..f3ba11d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1681,6 +1681,31 @@
 
 AM_CONDITIONAL(HAS_WRITE_STRINGS_WARNING, test x$no_write_strings = xyes)
 
+
+# does this compiler support -Wformat -Werror=format-security ?
+AC_MSG_CHECKING([if gcc accepts -Wformat -Werror=format-security])
+
+safe_CFLAGS=$CFLAGS
+CFLAGS="-Wformat -Werror=format-security"
+
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[
+  return 0;
+]])], [
+error_format_security=yes
+AC_MSG_RESULT([yes])
+], [
+error_format_security=no
+AC_MSG_RESULT([no])
+])
+CFLAGS=$safe_CFLAGS
+
+if test x$no_write_strings = xyes; then
+  CFLAGS="$CFLAGS -Wformat -Werror=format-security"
+  CXXFLAGS="$CXXFLAGS -Wformat -Werror=format-security"
+fi
+
+AM_CONDITIONAL(HAS_ERROR_FORMAT_SECURITY, test x$error_format_security = xyes)
+
 # does this compiler support -Wno-empty-body ?
 
 AC_MSG_CHECKING([if gcc accepts -Wno-empty-body])
diff --git a/coregrind/m_gdbserver/remote-utils.c b/coregrind/m_gdbserver/remote-utils.c
index d8dbe2a..b832d69 100644
--- a/coregrind/m_gdbserver/remote-utils.c
+++ b/coregrind/m_gdbserver/remote-utils.c
@@ -39,7 +39,7 @@
 static
 void sr_extended_perror (SysRes sr, const HChar *msg)
 {
-   sr_perror (sr, msg);
+   sr_perror (sr, "%s", msg);
    if (VG_(clo_verbosity) > 0 || VG_(debugLog_getLevel)() >= 1) {
       Int i;
       vki_sigset_t cursigset;