Skip to content

Commit 88a6b35

Browse files
author
David Keller
committed
Ensure XSI-compliant strerror_r is used.
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]>
1 parent 4bcc619 commit 88a6b35

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

native/dispatch.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
* Lesser General Public License for more details.
1717
*/
1818

19+
#include "dispatch.h"
20+
21+
#include <string.h>
22+
1923
#if defined(_WIN32)
2024
#define WIN32_LEAN_AND_MEAN
2125
#include <windows.h>
@@ -45,6 +49,7 @@
4549
#else
4650
#include <dlfcn.h>
4751
#include <errno.h>
52+
#include <assert.h>
4853
#define STRTYPE char*
4954
#ifdef USE_DEFAULT_LIBNAME_ENCODING
5055
#define NAME2CSTR(ENV,JSTR) newCString(ENV,JSTR)
@@ -53,8 +58,21 @@
5358
#endif
5459
#define DEFAULT_LOAD_OPTS (RTLD_LAZY|RTLD_GLOBAL)
5560
#define LOAD_LIBRARY(NAME,OPTS) dlopen(NAME, OPTS)
56-
#define LOAD_ERROR(BUF,LEN) (snprintf(BUF, LEN, "%s", dlerror()), BUF)
57-
#define STR_ERROR(CODE,BUF,LEN) (strerror_r(CODE, BUF, LEN), BUF)
61+
static inline char * LOAD_ERROR(char * buf, size_t len) {
62+
const size_t count = snprintf(buf, len, "%s", dlerror());
63+
assert(count <= len && "snprintf() output has been truncated");
64+
return buf;
65+
}
66+
static inline char * STR_ERROR(int code, char * buf, size_t len) {
67+
// The conversion will fail if code is not a valid error code.
68+
int err = strerror_r(code, buf, len);
69+
if (err)
70+
// Depending on glib version, "Unknown error" error code
71+
// may be returned or passed using errno.
72+
err = strerror_r(err > 0 ? err : errno, buf, len);
73+
assert(err == 0 && "strerror_r() conversion has failed");
74+
return buf;
75+
}
5876
#define FREE_LIBRARY(HANDLE) dlclose(HANDLE)
5977
#define FIND_ENTRY(HANDLE, NAME) dlsym(HANDLE, NAME)
6078
#endif
@@ -67,16 +85,10 @@
6785
#endif
6886

6987
#include <stdlib.h>
70-
// Force XSI-compliant strerror_r (http://unixhelp.ed.ac.uk/CGI/man-cgi?strerror)
71-
#ifndef _XOPEN_SOURCE
72-
#define _XOPEN_SOURCE 600
73-
#endif
74-
#include <string.h>
88+
#include <alloca.h>
7589
#include <wchar.h>
7690
#include <jni.h>
7791

78-
#include "dispatch.h"
79-
8092
#ifndef NO_JAWT
8193
#include <jawt.h>
8294
#include <jawt_md.h>

native/dispatch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#define GET_LAST_ERROR() GetLastError()
3737
#define SET_LAST_ERROR(CODE) SetLastError(CODE)
3838
#else
39+
#define _XOPEN_SOURCE 600
3940
#define GET_LAST_ERROR() errno
4041
#define SET_LAST_ERROR(CODE) (errno = (CODE))
4142
#endif /* _WIN32 */

0 commit comments

Comments
 (0)