diff --git a/native/dispatch.c b/native/dispatch.c index 4060ce99bf..ad8aa6b65a 100644 --- a/native/dispatch.c +++ b/native/dispatch.c @@ -16,6 +16,10 @@ * Lesser General Public License for more details. */ +#include "dispatch.h" + +#include + #if defined(_WIN32) #define WIN32_LEAN_AND_MEAN #include @@ -45,6 +49,7 @@ #else #include #include +#include #define STRTYPE char* #ifdef USE_DEFAULT_LIBNAME_ENCODING #define NAME2CSTR(ENV,JSTR) newCString(ENV,JSTR) @@ -53,8 +58,21 @@ #endif #define DEFAULT_LOAD_OPTS (RTLD_LAZY|RTLD_GLOBAL) #define LOAD_LIBRARY(NAME,OPTS) dlopen(NAME, OPTS) -#define LOAD_ERROR(BUF,LEN) (snprintf(BUF, LEN, "%s", dlerror()), BUF) -#define STR_ERROR(CODE,BUF,LEN) (strerror_r(CODE, BUF, LEN), BUF) +static inline char * LOAD_ERROR(char * buf, size_t len) { + const size_t count = snprintf(buf, len, "%s", dlerror()); + assert(count <= len && "snprintf() output has been truncated"); + return buf; +} +static inline char * STR_ERROR(int code, char * buf, size_t len) { + // The conversion will fail if code is not a valid error code. + int err = strerror_r(code, buf, len); + if (err) + // Depending on glib version, "Unknown error" error code + // may be returned or passed using errno. + err = strerror_r(err > 0 ? err : errno, buf, len); + assert(err == 0 && "strerror_r() conversion has failed"); + return buf; +} #define FREE_LIBRARY(HANDLE) dlclose(HANDLE) #define FIND_ENTRY(HANDLE, NAME) dlsym(HANDLE, NAME) #endif @@ -67,16 +85,10 @@ #endif #include -// Force XSI-compliant strerror_r (http://unixhelp.ed.ac.uk/CGI/man-cgi?strerror) -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif -#include +#include #include #include -#include "dispatch.h" - #ifndef NO_JAWT #include #include diff --git a/native/dispatch.h b/native/dispatch.h index 913535a323..4cafba34b6 100644 --- a/native/dispatch.h +++ b/native/dispatch.h @@ -36,6 +36,7 @@ #define GET_LAST_ERROR() GetLastError() #define SET_LAST_ERROR(CODE) SetLastError(CODE) #else +#define _XOPEN_SOURCE 600 #define GET_LAST_ERROR() errno #define SET_LAST_ERROR(CODE) (errno = (CODE)) #endif /* _WIN32 */