From 9188142698d8fdcee43c0b0519fde5c63b0010f8 Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Tue, 6 Jun 2023 15:15:32 +0000 Subject: [PATCH] 8309216: Cast from jchar* to char* in test java/io/GetXSpace.java Reviewed-by: rriggs, naoto --- test/jdk/java/io/File/libGetXSpace.c | 30 ++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/test/jdk/java/io/File/libGetXSpace.c b/test/jdk/java/io/File/libGetXSpace.c index 5f7643ea747..7a6cdf0d1dc 100644 --- a/test/jdk/java/io/File/libGetXSpace.c +++ b/test/jdk/java/io/File/libGetXSpace.c @@ -60,8 +60,8 @@ Java_GetXSpace_getSpace0 { jboolean totalSpaceIsEstimated = JNI_FALSE; jlong array[4]; - const jchar* chars = (*env)->GetStringChars(env, root, NULL); - if (chars == NULL) { + const jchar* strchars = (*env)->GetStringChars(env, root, NULL); + if (strchars == NULL) { JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException", "GetStringChars"); return JNI_FALSE; @@ -77,12 +77,13 @@ Java_GetXSpace_getSpace0 } } - LPCWSTR path = (LPCWSTR)chars; + LPCWSTR path = (LPCWSTR)strchars; if (pfnGetDiskSpaceInformation != NULL) { // use GetDiskSpaceInformationW DISK_SPACE_INFORMATION diskSpaceInfo; BOOL hres = pfnGetDiskSpaceInformation(path, &diskSpaceInfo); + (*env)->ReleaseStringChars(env, root, strchars); if (FAILED(hres)) { JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException", "GetDiskSpaceInformationW"); @@ -108,8 +109,10 @@ Java_GetXSpace_getSpace0 ULARGE_INTEGER totalNumberOfBytes; ULARGE_INTEGER totalNumberOfFreeBytes; - if (GetDiskFreeSpaceExW(path, &freeBytesAvailable, &totalNumberOfBytes, - &totalNumberOfFreeBytes) == 0) { + BOOL hres = GetDiskFreeSpaceExW(path, &freeBytesAvailable, + &totalNumberOfBytes, &totalNumberOfFreeBytes); + (*env)->ReleaseStringChars(env, root, strchars); + if (FAILED(hres)) { JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException", "GetDiskFreeSpaceExW"); return totalSpaceIsEstimated; @@ -124,9 +127,24 @@ Java_GetXSpace_getSpace0 array[3] = (jlong)freeBytesAvailable.QuadPart; } #else + int len = (int)(*env)->GetStringLength(env, root); + char* chars = (char*)malloc((len + 1)*sizeof(char)); + if (chars == NULL) { + (*env)->ReleaseStringChars(env, root, strchars); + JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException", + "malloc"); + return JNI_FALSE; + } + + for (int i = 0; i < len; i++) { + chars[i] = (char)strchars[i]; + } + chars[len] = '\0'; + (*env)->ReleaseStringChars(env, root, strchars); + struct statfs buf; int result = statfs((const char*)chars, &buf); - (*env)->ReleaseStringChars(env, root, chars); + free(chars); if (result < 0) { JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException", strerror(errno));