From: Alexandru Ardelean Date: Thu, 13 Jul 2017 07:11:15 +0000 (+0300) Subject: build: make `strerror()` override-able X-Git-Tag: json-c-0.13-20171207~72^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fb72160cafc1e5f0aeb702ba76428a992b9e8792;p=thirdparty%2Fjson-c.git build: make `strerror()` override-able If we want to override `strerror()` in libjson-c to make tests consistent across platforms, we need to do it build-wide as configure/build option. Apple linkers make it really hard to override functions at link-time, and this seems to be locked down on travis-ci.org [ for security reasons I assume ]. While I got it to work locally, it did not work when running on travis. Signed-off-by: Alexandru Ardelean --- diff --git a/Makefile.am b/Makefile.am index 126ceeb6..6f865250 100644 --- a/Makefile.am +++ b/Makefile.am @@ -53,6 +53,12 @@ libjson_c_la_SOURCES = \ printbuf.c \ random_seed.c +if ENABLE_STRERROR_OVERRIDE +libjson_cinclude_HEADERS+= \ + strerror_override.h +libjson_c_la_SOURCES+= \ + strerror_override.c +endif distclean-local: -rm -rf $(testsubdir) diff --git a/configure.ac b/configure.ac index bbaacf51..7af60bf5 100644 --- a/configure.ac +++ b/configure.ac @@ -23,6 +23,22 @@ else AC_MSG_RESULT([RDRAND Hardware RNG Hash Seed disabled. Use --enable-rdrand to enable]) fi +AC_ARG_ENABLE(strerror-override, + AS_HELP_STRING([--enable-strerror-override], + [Override strerror() function with internal version.]), +[if test x$enableval = xyes; then + enable_strerror_override=yes + AC_DEFINE(ENABLE_STRERROR_OVERRIDE, 1, [Override strerror() with internal version]) +fi]) + +AM_CONDITIONAL([ENABLE_STRERROR_OVERRIDE], [test "x$enable_strerror_override" = "xyes"]) + +if test "x$enable_strerror_override" = "xyes"; then + AC_MSG_RESULT([Overriding `strerror()` function with internal version]) +else + AC_MSG_RESULT([Using libc's `strerror()` function]) +fi + # enable silent build by default m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) diff --git a/json_object.c b/json_object.c index ddc96bc1..8c80426f 100644 --- a/json_object.c +++ b/json_object.c @@ -12,13 +12,14 @@ #include "config.h" +#include "strerror_override.h" + #include #include #include #include #include #include -#include #include "debug.h" #include "printbuf.h" diff --git a/json_pointer.c b/json_pointer.c index d106e9e9..2b2a9ef5 100644 --- a/json_pointer.c +++ b/json_pointer.c @@ -8,10 +8,11 @@ #include "config.h" +#include "strerror_override.h" + #include #include #include -#include #include #include diff --git a/json_util.c b/json_util.c index 9a2f9ff5..3a717b79 100644 --- a/json_util.c +++ b/json_util.c @@ -12,13 +12,14 @@ #include "config.h" #undef realloc +#include "strerror_override.h" + #include #include #include #include #include #include -#include #include #ifdef HAVE_SYS_TYPES_H diff --git a/random_seed.c b/random_seed.c index 3b61b770..cb086d3b 100644 --- a/random_seed.c +++ b/random_seed.c @@ -9,6 +9,7 @@ * */ +#include "strerror_override.h" #include #include "config.h" #include "random_seed.h" @@ -128,7 +129,6 @@ retry: #include #include #include -#include #include #include diff --git a/tests/strerror_override.c b/strerror_override.c similarity index 96% rename from tests/strerror_override.c rename to strerror_override.c index 980cbb9c..6cad0c09 100644 --- a/tests/strerror_override.c +++ b/strerror_override.c @@ -1,4 +1,4 @@ -#include +#include "strerror_override.h" /* * Override strerror() to get consistent output across platforms. @@ -54,7 +54,7 @@ static struct { #define PREFIX "ERRNO=" static char errno_buf[128] = PREFIX; -char *strerror(int errno_in) +char *_json_c_strerror(int errno_in) { int start_idx; char digbuf[20]; diff --git a/strerror_override.h b/strerror_override.h new file mode 100644 index 00000000..96e6bc6d --- /dev/null +++ b/strerror_override.h @@ -0,0 +1,12 @@ +#ifndef __STRERROR_OVERRIDE_H__ +#define __STRERROR_OVERRIDE_H__ + +#include "config.h" +#include + +#if ENABLE_STRERROR_OVERRIDE +char *_json_c_strerror(int errno_in); +#define strerror _json_c_strerror +#endif + +#endif /* __STRERROR_OVERRIDE_H__ */ diff --git a/tests/Makefile.am b/tests/Makefile.am index 824ed304..917d20f6 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -52,7 +52,7 @@ EXTRA_DIST+= test2Formatted_plain.expected EXTRA_DIST+= test2Formatted_pretty.expected EXTRA_DIST+= test2Formatted_spaced.expected -test_util_file_SOURCES = test_util_file.c strerror_override.c +test_util_file_SOURCES = test_util_file.c testsubdir=testSubDir TESTS_ENVIRONMENT = top_builddir=$(top_builddir) diff --git a/tests/test_json_pointer.c b/tests/test_json_pointer.c index c3733dea..cc96ab93 100644 --- a/tests/test_json_pointer.c +++ b/tests/test_json_pointer.c @@ -1,4 +1,4 @@ -#include +#include "strerror_override.h" #include #include #include diff --git a/tests/test_util_file.c b/tests/test_util_file.c index 5dbdb477..b4918bee 100644 --- a/tests/test_util_file.c +++ b/tests/test_util_file.c @@ -1,4 +1,4 @@ -#include +#include "strerror_override.h" #include #include #include