Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #392 - Ensure XSI-compliant strerror_r is used. #669

Merged
merged 2 commits into from
Sep 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions native/dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
* Lesser General Public License for more details.
*/

#include "dispatch.h"

#include <string.h>

#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
Expand Down Expand Up @@ -45,6 +49,7 @@
#else
#include <dlfcn.h>
#include <errno.h>
#include <assert.h>
#define STRTYPE char*
#ifdef USE_DEFAULT_LIBNAME_ENCODING
#define NAME2CSTR(ENV,JSTR) newCString(ENV,JSTR)
Expand All @@ -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
Expand All @@ -67,16 +85,9 @@
#endif

#include <stdlib.h>
// Force XSI-compliant strerror_r (http://unixhelp.ed.ac.uk/CGI/man-cgi?strerror)
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif
#include <string.h>
#include <wchar.h>
#include <jni.h>

#include "dispatch.h"

#ifndef NO_JAWT
#include <jawt.h>
#include <jawt_md.h>
Expand Down
3 changes: 2 additions & 1 deletion native/dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "ffi.h"
#include "com_sun_jna_Function.h"
#include "com_sun_jna_Native.h"
#if defined(__sun__) || defined(_AIX)
#if defined(__sun__) || defined(_AIX) || defined(__linux__)
# include <alloca.h>
#endif
#ifdef _WIN32
Expand All @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions native/protect.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#ifdef __GNUC__
#include <excpt.h>
#else
#include <windows.h>
// copied from mingw header
typedef EXCEPTION_DISPOSITION (*PEXCEPTION_HANDLER)
(struct _EXCEPTION_RECORD*, void*, struct _CONTEXT*, void*);
Expand Down
2 changes: 2 additions & 0 deletions native/snprintf.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef _SNPRINTF_H
#define _SNPRINTF_H
#if _MSC_VER < 1900 // Before Visual Studio 2015
// snprintf on windows is broken; always nul-terminate manually
// DO NOT rely on the return value...
static int snprintf(char * str, size_t size, const char * format, ...) {
Expand All @@ -10,4 +11,5 @@ static int snprintf(char * str, size_t size, const char * format, ...) {
va_end(ap);
return retval;
}
#endif
#endif /* _SNPRINTF_H */