From 6bef143155f02d2f6b85ee1d80cf37d835b2518c Mon Sep 17 00:00:00 2001 From: Kostas Tsiounis Date: Mon, 9 Dec 2024 13:42:37 -0500 Subject: [PATCH] Address review comments 2 --- .../share/native/libjncrypto/NativeCrypto.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/closed/src/java.base/share/native/libjncrypto/NativeCrypto.c b/closed/src/java.base/share/native/libjncrypto/NativeCrypto.c index df2ae608b7c..365d0d90bcf 100644 --- a/closed/src/java.base/share/native/libjncrypto/NativeCrypto.c +++ b/closed/src/java.base/share/native/libjncrypto/NativeCrypto.c @@ -675,19 +675,24 @@ find_crypto_library(jboolean traceEnabled, const char *chomepath) /* If JAVA_HOME is not null or empty and no library has been loaded yet, try there. */ if ((NULL != chomepath) && ('\0' != *chomepath) && (NULL == crypto_library)) { - size_t path_len = strlen(chomepath); #if defined(_WIN32) static const char pathSuffix[] = "\\bin\\"; #else /* defined(_WIN32) */ static const char pathSuffix[] = "/lib/"; #endif /* defined(_WIN32) */ - char *libPath = malloc(path_len + sizeof(pathSuffix)); + size_t path_len = strlen(chomepath) + sizeof(pathSuffix) - 1; + char *libPath = malloc(path_len + 1); + if (NULL == libPath) { + if (traceEnabled) { + fprintf(stderr, "\tFailed to allocate memory for path.\n"); + } + return NULL; + } strcpy(libPath, chomepath); /* Append the proper directory using a slash or backslash, depending on the operating system. */ strcat(libPath, pathSuffix); - size_t libPath_len = strlen(libPath); if (traceEnabled) { fprintf(stdout, "Attempting to load library bundled with JDK from: %s\n", libPath); } @@ -695,7 +700,7 @@ find_crypto_library(jboolean traceEnabled, const char *chomepath) for (i = 0; i < numOfLibs; i++) { size_t file_len = strlen(libNames[i]); /* Allocate memory for the new file name with the path. */ - char *libNameWithPath = (char *)malloc((libPath_len + file_len)); + char *libNameWithPath = (char *)malloc(path_len + file_len + 1); if (NULL == libNameWithPath) { if (traceEnabled) { @@ -713,9 +718,7 @@ find_crypto_library(jboolean traceEnabled, const char *chomepath) } result = load_crypto_library(traceEnabled, libNameWithPath); - if (NULL != libNameWithPath) { - free(libNameWithPath); - } + free(libNameWithPath); if (NULL == result) { continue;