Add ./configure check for vsnprintf returning negative on error
authorMike Kaplinskiy <mikekap@vineapp.com>
Mon, 22 Jun 2015 23:15:24 +0000 (16:15 -0700)
committerSara Golemon <sgolemon@fb.com>
Mon, 22 Jun 2015 23:45:17 +0000 (16:45 -0700)
Summary: This is slightly more correct than assuming that
__APPLE__ is the only place this assumption breaks.

Pulled from relevant section of folly-PR#95, other sections
are either already applied by other fixes, or have been code-moved.

Closes #95

Reviewed By: @jwatzman

Differential Revision: D2178439

folly/configure.ac
folly/test/StringTest.cpp

index 8b975066c5d6fe5bdfb317992241439e9139f06c..3a4311a3e86539ac3b8e4adf7d5d0aac6a7a0fe3 100644 (file)
@@ -253,6 +253,25 @@ if test "$folly_cv_prog_cc_weak_symbols" = yes; then
             [Define to 1 if the linker supports weak symbols.])
 fi
 
             [Define to 1 if the linker supports weak symbols.])
 fi
 
+AC_CACHE_CHECK(
+  [for vsnprintf reporting bad format strings],
+  [folly_cv_prog_vsnprintf_bad_format],
+  [AC_RUN_IFELSE(
+    [AC_LANG_SOURCE[
+      #include <stdio.h>
+
+      int main(int argc, char** argv) {
+          char buf[256];
+          return vsnprintf(buf, sizeof(buf), "%", 1) < 0 ? 0 : 1;
+      }]],
+    [folly_cv_prog_vsnprintf_bad_format="yes"],
+    [folly_cv_prog_vsnprintf_bad_format="no"])])
+
+if test "$folly_cv_prog_vsnprintf_bad_format" = yes; then
+  AC_DEFINE([HAVE_VSNPRINTF_ERRORS], [1],
+            [Define to 1 if the vsnprintf supports returning errors on bad format strings.])
+fi
+
 AC_SEARCH_LIBS([cplus_demangle_v3_callback], [iberty_pic iberty])
 if test "$ac_cv_search_cplus_demangle_v3_callback" != "no" ; then
   AC_DEFINE([HAVE_CPLUS_DEMANGLE_V3_CALLBACK], [1],
 AC_SEARCH_LIBS([cplus_demangle_v3_callback], [iberty_pic iberty])
 if test "$ac_cv_search_cplus_demangle_v3_callback" != "no" ; then
   AC_DEFINE([HAVE_CPLUS_DEMANGLE_V3_CALLBACK], [1],
index bda025b7ec003820926d84a8dd2c5fc2eb3bb3e3..3c5ecf314709b69f9f38873d142fbdb72a09f6d7 100644 (file)
@@ -102,7 +102,7 @@ void vprintfError(const char* fmt, ...) {
   // OSX's sprintf family does not return a negative number on a bad format
   // string, but Linux does. It's unclear to me which behavior is more
   // correct.
   // OSX's sprintf family does not return a negative number on a bad format
   // string, but Linux does. It's unclear to me which behavior is more
   // correct.
-#if !__APPLE__
+#ifdef HAVE_VSNPRINTF_ERRORS
   EXPECT_THROW({stringVPrintf(fmt, ap);},
                std::runtime_error);
 #endif
   EXPECT_THROW({stringVPrintf(fmt, ap);},
                std::runtime_error);
 #endif