Skip to content

Commit

Permalink
Centralise error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbASF committed Nov 6, 2023
1 parent c3750c5 commit 950bf17
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 33 deletions.
16 changes: 16 additions & 0 deletions src/main/native/org/apache/commons/crypto/DynamicLoader.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ HMODULE open_library(JNIEnv *env)
#endif

}
// Did we succeed?
if (!openssl)
{
char msg[1000];
#ifdef UNIX
snprintf(msg, sizeof(msg), "Cannot load %s (%s)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \
dlerror()); // returns char*
#endif
#ifdef WINDOWS
// TODO: convert to string
snprintf(msg, sizeof(msg), "Cannot load %s (%d)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \
GetLastError()); // returns DWORD
#endif
THROW(env, "java/lang/UnsatisfiedLinkError", msg);
return 0;
}
return openssl;
}

Expand Down
12 changes: 1 addition & 11 deletions src/main/native/org/apache/commons/crypto/OpenSslInfoNative.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,9 @@ static void get_methods(JNIEnv *env, HMODULE openssl)

static int load_library(JNIEnv *env)
{
HMODULE openssl = open_library(env);
HMODULE openssl = open_library(env); // calls THROW and returns 0 on error

if (!openssl) {
char msg[1000];
#ifdef UNIX
snprintf(msg, sizeof(msg), "Cannot load %s (%s)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \
dlerror()); // returns char*
#endif
#ifdef WINDOWS
snprintf(msg, sizeof(msg), "Cannot load %s (%d)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \
GetLastError()); // returns DWORD
#endif
THROW(env, "java/lang/UnsatisfiedLinkError", msg);
return 0;
}
get_methods(env, openssl);
Expand Down
12 changes: 1 addition & 11 deletions src/main/native/org/apache/commons/crypto/cipher/OpenSslNative.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,9 @@ static void loadAes(JNIEnv *env, HMODULE openssl)
JNIEXPORT void JNICALL Java_org_apache_commons_crypto_cipher_OpenSslNative_initIDs
(JNIEnv *env, jclass clazz)
{
HMODULE openssl = open_library(env);
HMODULE openssl = open_library(env); // calls THROW and returns 0 on error

if (!openssl) {
char msg[1000];
#ifdef UNIX
snprintf(msg, sizeof(msg), "Cannot load %s (%s)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \
dlerror()); // returns char*
#endif
#ifdef WINDOWS
snprintf(msg, sizeof(msg), "Cannot load %s (%d)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \
GetLastError()); // returns DWORD
#endif
THROW(env, "java/lang/UnsatisfiedLinkError", msg);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,12 @@ static int openssl_rand_bytes(unsigned char *buf, int num);

JNIEXPORT void JNICALL Java_org_apache_commons_crypto_random_OpenSslCryptoRandomNative_initSR (JNIEnv *env, jclass clazz)
{
HMODULE openssl = open_library(env);
HMODULE openssl = open_library(env); // calls THROW and returns 0 on error

if (!openssl) {
char msg[1000];
#ifdef UNIX
snprintf(msg, sizeof(msg), "Cannot load %s (%s)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \
dlerror()); // returns char*
#endif
#ifdef WINDOWS
snprintf(msg, sizeof(msg), "Cannot load %s (%d)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \
GetLastError()); // returns DWORD
#endif
THROW(env, "java/lang/UnsatisfiedLinkError", msg);
return;
}

LOAD_DYNAMIC_SYMBOL_FALLBACK(__dlsym_OpenSSL_version_num, dlsym_OpenSSL_version_num, env, openssl, "OpenSSL_version_num", "SSLeay");
#ifdef UNIX
dlerror(); // Clear any existing error
Expand Down

0 comments on commit 950bf17

Please sign in to comment.