Skip to content

Commit

Permalink
Merge pull request #1411 from edenhill/safe_strerror
Browse files Browse the repository at this point in the history
Thread-safe rd_strerror() (#1410)
  • Loading branch information
edenhill committed Sep 5, 2017
2 parents fee1e42 + 33e262c commit 61d786b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
11 changes: 11 additions & 0 deletions configure.librdkafka
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ int foo (void) {
return strndup(\"hi\", 2) ? 0 : 1;
}"

# Check if strerror_r() is available.
# The check for GNU vs XSI is done in rdposix.h since
# we can't rely on all defines to be set here (_GNU_SOURCE).
mkl_compile_check "strerror_r" "HAVE_STRERROR_R" disable CC "" \
"#include <string.h>
const char *foo (void) {
static char buf[64];
strerror_r(1, buf, sizeof(buf));
return buf;
}"

# Check if dlopen() is available
mkl_lib_check "libdl" "WITH_LIBDL" disable CC "-ldl" \
"
Expand Down
20 changes: 20 additions & 0 deletions src/rdposix.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,27 @@
/**
* Errors
*/
#if HAVE_STRERROR_R
static RD_INLINE RD_UNUSED const char *rd_strerror(int err) {
static RD_TLS char ret[128];

#if defined(__linux__) && defined(_GNU_SOURCE)
return strerror_r(err, ret, sizeof(ret));
#else /* XSI version */
int r;
/* The r assignment is to catch the case where
* _GNU_SOURCE is not defined but the GNU version is
* picked up anyway. */
r = strerror_r(err, ret, sizeof(ret));
if (unlikely(r))
rd_snprintf(ret, sizeof(ret),
"strerror_r(%d) failed (ret %d)", err, r);
return ret;
#endif
}
#else
#define rd_strerror(err) strerror(err)
#endif


/**
Expand Down

0 comments on commit 61d786b

Please sign in to comment.