Skip to content

Commit

Permalink
Ensure XSI-compliant strerror_r is used.
Browse files Browse the repository at this point in the history
This macro was used after some standard header includes.
Depending on implementation, if one of theses headers were using
<string.h>, the later presence of _XOPEN_SOURCE macro would'nt matter.

This prevents uninitialized buffer from being passed to snprintf.

Signed-off-by: David Keller <[email protected]>
  • Loading branch information
David Keller committed Sep 8, 2016
1 parent 4bcc619 commit 88a6b35
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
30 changes: 21 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,10 @@
#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 <alloca.h>
#include <wchar.h>
#include <jni.h>

#include "dispatch.h"

#ifndef NO_JAWT
#include <jawt.h>
#include <jawt_md.h>
Expand Down
1 change: 1 addition & 0 deletions native/dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 88a6b35

Please sign in to comment.