Skip to content

Commit

Permalink
Fix: see android/ndk#339
Browse files Browse the repository at this point in the history
  • Loading branch information
1951FDG committed Sep 13, 2019
1 parent 739f743 commit 8734083
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
31 changes: 24 additions & 7 deletions app/src/main/jni/sqlite-android/JNIHelp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,36 @@ void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable ex
__android_log_write(priority, tag, trace.c_str());
}

const char* jniStrError(int errnum, char* buf, size_t buflen) {
#if 1
// Note: glibc has a nonstandard strerror_r that returns char* rather than POSIX's int.
// char *strerror_r(int errnum, char *buf, size_t n);
return strerror_r(errnum, buf, buflen);
#else
int rc = strerror_r(errnum, buf, buflen);
// Note: glibc has a nonstandard strerror_r that returns char* rather than POSIX's int.
// char *strerror_r(int errnum, char *buf, size_t n);
//
// Some versions of bionic support the glibc style call. Since the set of defines that determine
// which version is used is byzantine in its complexity we will just use this C++ template hack to
// select the correct jniStrError implementation based on the libc being used.
namespace impl {
using GNUStrError = char* (*)(int,char*,size_t);
using POSIXStrError = int (*)(int,char*,size_t);
inline const char* realJniStrError(GNUStrError func, int errnum, char* buf, size_t buflen) {
return func(errnum, buf, buflen);
}
inline const char* realJniStrError(POSIXStrError func, int errnum, char* buf, size_t buflen) {
int rc = func(errnum, buf, buflen);
if (rc != 0) {
// (POSIX only guarantees a value other than 0. The safest
// way to implement this function is to use C++ and overload on the
// type of strerror_r to accurately distinguish GNU from POSIX.)
snprintf(buf, buflen, "errno %d", errnum);
}
return buf;
}
} // namespace impl
const char* jniStrError(int errnum, char* buf, size_t buflen) {
#ifdef _WIN32
strerror_s(buf, buflen, errnum);
return buf;
#else
// The magic of C++ overloading selects the correct implementation based on the declared type of
// strerror_r. The inline will ensure that we don't have any indirect calls.
return impl::realJniStrError(strerror_r, errnum, buf, buflen);
#endif
}
31 changes: 24 additions & 7 deletions app/src/main/jni/sqlite/JNIHelp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,20 +288,37 @@ void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable ex
__android_log_write(priority, tag, trace.c_str());
}

const char* jniStrError(int errnum, char* buf, size_t buflen) {
#if 1
// Note: glibc has a nonstandard strerror_r that returns char* rather than POSIX's int.
// char *strerror_r(int errnum, char *buf, size_t n);
return strerror_r(errnum, buf, buflen);
#else
int rc = strerror_r(errnum, buf, buflen);
// Note: glibc has a nonstandard strerror_r that returns char* rather than POSIX's int.
// char *strerror_r(int errnum, char *buf, size_t n);
//
// Some versions of bionic support the glibc style call. Since the set of defines that determine
// which version is used is byzantine in its complexity we will just use this C++ template hack to
// select the correct jniStrError implementation based on the libc being used.
namespace impl {
using GNUStrError = char* (*)(int,char*,size_t);
using POSIXStrError = int (*)(int,char*,size_t);
inline const char* realJniStrError(GNUStrError func, int errnum, char* buf, size_t buflen) {
return func(errnum, buf, buflen);
}
inline const char* realJniStrError(POSIXStrError func, int errnum, char* buf, size_t buflen) {
int rc = func(errnum, buf, buflen);
if (rc != 0) {
// (POSIX only guarantees a value other than 0. The safest
// way to implement this function is to use C++ and overload on the
// type of strerror_r to accurately distinguish GNU from POSIX.)
snprintf(buf, buflen, "errno %d", errnum);
}
return buf;
}
} // namespace impl
const char* jniStrError(int errnum, char* buf, size_t buflen) {
#ifdef _WIN32
strerror_s(buf, buflen, errnum);
return buf;
#else
// The magic of C++ overloading selects the correct implementation based on the declared type of
// strerror_r. The inline will ensure that we don't have any indirect calls.
return impl::realJniStrError(strerror_r, errnum, buf, buflen);
#endif
}

Expand Down

0 comments on commit 8734083

Please sign in to comment.