From 0b0309624a5c13f6059a647a73b3c6d9799c604f Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 26 May 2017 13:54:53 +0200 Subject: [PATCH] Correctly parse package-prefix if the jni lib is contained in a path that matches the library name. Motivation: We used strstr to find the path to the library, which fails if the library is contained in a directory that also matches the library name. Modifications: - Introduce netty_unix_util_strstr_last which will return a pointer which points to the last accourance and so not fails if the direct also matches the library name. Result: Be able to load the library in all cases. --- .../src/main/c/netty_unix_util.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/transport-native-unix-common/src/main/c/netty_unix_util.c b/transport-native-unix-common/src/main/c/netty_unix_util.c index 903ae607cf..7857b02f43 100644 --- a/transport-native-unix-common/src/main/c/netty_unix_util.c +++ b/transport-native-unix-common/src/main/c/netty_unix_util.c @@ -51,8 +51,20 @@ char* netty_unix_util_rstrstr(char* s1rbegin, const char* s1rend, const char* s2 return NULL; } +static char* netty_unix_util_strstr_last(const char* haystack, const char* needle) { + char* prevptr = NULL; + char* ptr = (char*) haystack; + + while ((ptr = strstr(ptr, needle)) != NULL) { + // Just store the ptr and continue searching. + prevptr = ptr; + ++ptr; + } + return prevptr; +} + char* netty_unix_util_parse_package_prefix(const char* libraryPathName, const char* libraryName, jint* status) { - char* packageNameEnd = strstr(libraryPathName, libraryName); + char* packageNameEnd = netty_unix_util_strstr_last(libraryPathName, libraryName); if (packageNameEnd == NULL) { *status = JNI_ERR; return NULL;