Skip to content

Commit

Permalink
build: make strerror() override-able
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
commodo committed Jul 13, 2017
1 parent 85f5785 commit fb72160
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 9 deletions.
6 changes: 6 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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])])

Expand Down
3 changes: 2 additions & 1 deletion json_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@

#include "config.h"

#include "strerror_override.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <math.h>
#include <errno.h>

#include "debug.h"
#include "printbuf.h"
Expand Down
3 changes: 2 additions & 1 deletion json_pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

#include "config.h"

#include "strerror_override.h"

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>

Expand Down
3 changes: 2 additions & 1 deletion json_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
#include "config.h"
#undef realloc

#include "strerror_override.h"

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>

#ifdef HAVE_SYS_TYPES_H
Expand Down
2 changes: 1 addition & 1 deletion random_seed.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
*/

#include "strerror_override.h"
#include <stdio.h>
#include "config.h"
#include "random_seed.h"
Expand Down Expand Up @@ -128,7 +129,6 @@ static int get_rdrand_seed()
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>

Expand Down
4 changes: 2 additions & 2 deletions tests/strerror_override.c → strerror_override.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <errno.h>
#include "strerror_override.h"

/*
* Override strerror() to get consistent output across platforms.
Expand Down Expand Up @@ -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];
Expand Down
12 changes: 12 additions & 0 deletions strerror_override.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef __STRERROR_OVERRIDE_H__
#define __STRERROR_OVERRIDE_H__

#include "config.h"
#include <errno.h>

#if ENABLE_STRERROR_OVERRIDE
char *_json_c_strerror(int errno_in);
#define strerror _json_c_strerror
#endif

#endif /* __STRERROR_OVERRIDE_H__ */
2 changes: 1 addition & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_json_pointer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <errno.h>
#include "strerror_override.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
Expand Down
2 changes: 1 addition & 1 deletion tests/test_util_file.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <errno.h>
#include "strerror_override.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
Expand Down

0 comments on commit fb72160

Please sign in to comment.