diff --git a/src/java.base/share/native/libjava/io_util.c b/src/java.base/share/native/libjava/io_util.c index 70b20157f6e..3fb7675d277 100644 --- a/src/java.base/share/native/libjava/io_util.c +++ b/src/java.base/share/native/libjava/io_util.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -204,16 +204,11 @@ writeBytes(JNIEnv *env, jobject this, jbyteArray bytes, void throwFileNotFoundException(JNIEnv *env, jstring path) { - char buf[256]; - size_t n; jobject x; - jstring why = NULL; + jstring why; - n = getLastErrorString(buf, sizeof(buf)); - if (n > 0) { - why = JNU_NewStringPlatform(env, buf); - CHECK_NULL(why); - } + why = getLastErrorString(env); + JNU_CHECK_EXCEPTION(env); x = JNU_NewObjectByName(env, "java/io/FileNotFoundException", "(Ljava/lang/String;Ljava/lang/String;)V", diff --git a/src/java.base/share/native/libjava/jni_util.c b/src/java.base/share/native/libjava/jni_util.c index a4ecbbc6cdf..d6e3176c14d 100644 --- a/src/java.base/share/native/libjava/jni_util.c +++ b/src/java.base/share/native/libjava/jni_util.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -103,17 +103,13 @@ JNIEXPORT void JNICALL JNU_ThrowByNameWithLastError(JNIEnv *env, const char *name, const char *defaultDetail) { - char buf[256]; - size_t n = getLastErrorString(buf, sizeof(buf)); + jstring s = getLastErrorString(env); - if (n > 0) { - jstring s = JNU_NewStringPlatform(env, buf); - if (s != NULL) { - jobject x = JNU_NewObjectByName(env, name, - "(Ljava/lang/String;)V", s); - if (x != NULL) { - (*env)->Throw(env, x); - } + if (s != NULL) { + jobject x = JNU_NewObjectByName(env, name, + "(Ljava/lang/String;)V", s); + if (x != NULL) { + (*env)->Throw(env, x); } } if (!(*env)->ExceptionOccurred(env)) { @@ -129,48 +125,44 @@ JNIEXPORT void JNICALL JNU_ThrowByNameWithMessageAndLastError (JNIEnv *env, const char *name, const char *message) { - char buf[256]; - size_t n = getLastErrorString(buf, sizeof(buf)); size_t messagelen = message == NULL ? 0 : strlen(message); - if (n > 0) { - jstring s = JNU_NewStringPlatform(env, buf); - if (s != NULL) { - jobject x = NULL; - if (messagelen) { - jstring s2 = NULL; - size_t messageextlen = messagelen + 4; - char *str1 = (char *)malloc((messageextlen) * sizeof(char)); - if (str1 == 0) { - JNU_ThrowOutOfMemoryError(env, 0); - return; - } - jio_snprintf(str1, messageextlen, " (%s)", message); - s2 = (*env)->NewStringUTF(env, str1); - free(str1); + jstring s = getLastErrorString(env); + if (s != NULL) { + jobject x = NULL; + if (messagelen > 0) { + jstring s2 = NULL; + size_t messageextlen = messagelen + 4; + char *str1 = (char *)malloc((messageextlen) * sizeof(char)); + if (str1 == NULL) { + JNU_ThrowOutOfMemoryError(env, 0); + return; + } + jio_snprintf(str1, messageextlen, " (%s)", message); + s2 = (*env)->NewStringUTF(env, str1); + free(str1); + JNU_CHECK_EXCEPTION(env); + if (s2 != NULL) { + jstring s3 = JNU_CallMethodByName( + env, NULL, s, "concat", + "(Ljava/lang/String;)Ljava/lang/String;", + s2).l; + (*env)->DeleteLocalRef(env, s2); JNU_CHECK_EXCEPTION(env); - if (s2 != NULL) { - jstring s3 = JNU_CallMethodByName( - env, NULL, s, "concat", - "(Ljava/lang/String;)Ljava/lang/String;", - s2).l; - (*env)->DeleteLocalRef(env, s2); - JNU_CHECK_EXCEPTION(env); - if (s3 != NULL) { - (*env)->DeleteLocalRef(env, s); - s = s3; - } + if (s3 != NULL) { + (*env)->DeleteLocalRef(env, s); + s = s3; } } - x = JNU_NewObjectByName(env, name, "(Ljava/lang/String;)V", s); - if (x != NULL) { - (*env)->Throw(env, x); - } + } + x = JNU_NewObjectByName(env, name, "(Ljava/lang/String;)V", s); + if (x != NULL) { + (*env)->Throw(env, x); } } if (!(*env)->ExceptionOccurred(env)) { - if (messagelen) { + if (messagelen > 0) { JNU_ThrowByName(env, name, message); } else { JNU_ThrowByName(env, name, "no further information"); diff --git a/src/java.base/share/native/libjava/jni_util.h b/src/java.base/share/native/libjava/jni_util.h index bd815e6f1b9..4fcc50a1870 100644 --- a/src/java.base/share/native/libjava/jni_util.h +++ b/src/java.base/share/native/libjava/jni_util.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -336,8 +336,7 @@ void* getProcessHandle(); void buildJniFunctionName(const char *sym, const char *cname, char *jniEntryName); -JNIEXPORT size_t JNICALL -getLastErrorString(char *buf, size_t len); +jstring getLastErrorString(JNIEnv *env); JNIEXPORT int JNICALL getErrorString(int err, char *buf, size_t len); diff --git a/src/java.base/share/native/libzip/zip_util.c b/src/java.base/share/native/libzip/zip_util.c index f327d8fe6bd..e4daedbe873 100644 --- a/src/java.base/share/native/libzip/zip_util.c +++ b/src/java.base/share/native/libzip/zip_util.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1995, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -529,7 +529,7 @@ addMetaName(jzfile *zip, const char *name, int length) static void freeMetaNames(jzfile *zip) { - if (zip->metanames) { + if (zip->metanames != NULL) { jint i; for (i = 0; i < zip->metacount; i++) free(zip->metanames[i]); @@ -764,7 +764,8 @@ readCEN(jzfile *zip, jint knownTotal) * Opens a zip file with the specified mode. Returns the jzfile object * or NULL if an error occurred. If a zip error occurred then *pmsg will * be set to the error message text if pmsg != 0. Otherwise, *pmsg will be - * set to NULL. Caller is responsible to free the error message. + * set to NULL. Caller doesn't need to free the error message. + * The error message, if set, points to a static thread-safe buffer. */ jzfile * ZIP_Open_Generic(const char *name, char **pmsg, int mode, jlong lastModified) @@ -790,7 +791,7 @@ ZIP_Open_Generic(const char *name, char **pmsg, int mode, jlong lastModified) * zip files, or NULL if the file is not in the cache. If the name is longer * than PATH_MAX or a zip error occurred then *pmsg will be set to the error * message text if pmsg != 0. Otherwise, *pmsg will be set to NULL. Caller - * is responsible to free the error message. + * doesn't need to free the error message. */ jzfile * ZIP_Get_From_Cache(const char *name, char **pmsg, jlong lastModified) @@ -803,13 +804,13 @@ ZIP_Get_From_Cache(const char *name, char **pmsg, jlong lastModified) } /* Clear zip error message */ - if (pmsg != 0) { + if (pmsg != NULL) { *pmsg = NULL; } if (strlen(name) >= PATH_MAX) { - if (pmsg) { - *pmsg = strdup("zip file name too long"); + if (pmsg != NULL) { + *pmsg = "zip file name too long"; } return NULL; } @@ -834,7 +835,7 @@ ZIP_Get_From_Cache(const char *name, char **pmsg, jlong lastModified) * Reads data from the given file descriptor to create a jzfile, puts the * jzfile in a cache, and returns that jzfile. Returns NULL in case of error. * If a zip error occurs, then *pmsg will be set to the error message text if - * pmsg != 0. Otherwise, *pmsg will be set to NULL. Caller is responsible to + * pmsg != 0. Otherwise, *pmsg will be set to NULL. Caller doesn't need to * free the error message. */ @@ -863,8 +864,8 @@ ZIP_Put_In_Cache0(const char *name, ZFILE zfd, char **pmsg, jlong lastModified, zip->lastModified = lastModified; if (zfd == -1) { - if (pmsg && getLastErrorString(errbuf, sizeof(errbuf)) > 0) - *pmsg = strdup(errbuf); + if (pmsg != NULL) + *pmsg = "ZFILE_Open failed"; freeZip(zip); return NULL; } @@ -877,12 +878,12 @@ ZIP_Put_In_Cache0(const char *name, ZFILE zfd, char **pmsg, jlong lastModified, len = zip->len = IO_Lseek(zfd, 0, SEEK_END); if (len <= 0) { if (len == 0) { /* zip file is empty */ - if (pmsg) { - *pmsg = strdup("zip file is empty"); + if (pmsg != NULL) { + *pmsg = "zip file is empty"; } } else { /* error */ - if (pmsg && getLastErrorString(errbuf, sizeof(errbuf)) > 0) - *pmsg = strdup(errbuf); + if (pmsg != NULL) + *pmsg = "IO_Lseek failed"; } ZFILE_Close(zfd); freeZip(zip); @@ -892,10 +893,9 @@ ZIP_Put_In_Cache0(const char *name, ZFILE zfd, char **pmsg, jlong lastModified, zip->zfd = zfd; if (readCEN(zip, -1) < 0) { /* An error occurred while trying to read the zip file */ - if (pmsg != 0) { + if (pmsg != NULL) { /* Set the zip error message */ - if (zip->msg != NULL) - *pmsg = strdup(zip->msg); + *pmsg = zip->msg; } freeZip(zip); return NULL; @@ -918,10 +918,6 @@ JNIEXPORT jzfile * ZIP_Open(const char *name, char **pmsg) { jzfile *file = ZIP_Open_Generic(name, pmsg, O_RDONLY, 0); - if (file == NULL && pmsg != NULL && *pmsg != NULL) { - free(*pmsg); - *pmsg = "Zip file open error"; - } return file; } @@ -1138,8 +1134,8 @@ ZIP_FreeEntry(jzfile *jz, jzentry *ze) if (last != NULL) { /* Free the previously cached jzentry */ free(last->name); - if (last->extra) free(last->extra); - if (last->comment) free(last->comment); + free(last->extra); + free(last->comment); free(last); } } @@ -1517,7 +1513,7 @@ ZIP_ReadEntry(jzfile *zip, jzentry *entry, unsigned char *buf, char *entryname) msg = zip->msg; ZIP_Unlock(zip); if (n == -1) { - if (msg == 0) { + if (msg == NULL) { getErrorString(errno, tmpbuf, sizeof(tmpbuf)); msg = tmpbuf; } @@ -1534,7 +1530,7 @@ ZIP_ReadEntry(jzfile *zip, jzentry *entry, unsigned char *buf, char *entryname) if ((msg == NULL) || (*msg == 0)) { msg = zip->msg; } - if (msg == 0) { + if (msg == NULL) { getErrorString(errno, tmpbuf, sizeof(tmpbuf)); msg = tmpbuf; } @@ -1554,7 +1550,7 @@ ZIP_InflateFully(void *inBuf, jlong inLen, void *outBuf, jlong outLen, char **pm z_stream strm; memset(&strm, 0, sizeof(z_stream)); - *pmsg = 0; /* Reset error message */ + *pmsg = NULL; /* Reset error message */ if (inflateInit2(&strm, MAX_WBITS) != Z_OK) { *pmsg = strm.msg; diff --git a/src/java.base/unix/native/libjava/jni_util_md.c b/src/java.base/unix/native/libjava/jni_util_md.c index 460503cd794..32b477aa7ac 100644 --- a/src/java.base/unix/native/libjava/jni_util_md.c +++ b/src/java.base/unix/native/libjava/jni_util_md.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -60,12 +60,13 @@ void buildJniFunctionName(const char *sym, const char *cname, } } -JNIEXPORT size_t JNICALL -getLastErrorString(char *buf, size_t len) +jstring +getLastErrorString(JNIEnv *env) { - if (errno == 0 || len < 1) return 0; - getErrorString(errno, buf, len); - return strlen(buf); + char buf[256] = {0}; + if (errno == 0) return NULL; + getErrorString(errno, buf, sizeof(buf)); + return (buf[0] != 0) ? JNU_NewStringPlatform(env, buf) : NULL; } JNIEXPORT int JNICALL diff --git a/src/java.base/windows/native/libjava/jni_util_md.c b/src/java.base/windows/native/libjava/jni_util_md.c index 9ed2d384aed..9c64a339aad 100644 --- a/src/java.base/windows/native/libjava/jni_util_md.c +++ b/src/java.base/windows/native/libjava/jni_util_md.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -63,34 +63,34 @@ void buildJniFunctionName(const char *sym, const char *cname, return; } -JNIEXPORT size_t JNICALL -getLastErrorString(char *buf, size_t len) { +jstring +getLastErrorString(JNIEnv *env) { +#define BUFSIZE 256 DWORD errval; + WCHAR buf[BUFSIZE]; if ((errval = GetLastError()) != 0) { // DOS error - size_t n = (size_t)FormatMessage( + jsize n = FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errval, 0, buf, - (DWORD)len, + BUFSIZE, NULL); if (n > 3) { // Drop final '.', CR, LF - if (buf[n - 1] == '\n') n--; - if (buf[n - 1] == '\r') n--; - if (buf[n - 1] == '.') n--; - buf[n] = '\0'; + if (buf[n - 1] == L'\n') n--; + if (buf[n - 1] == L'\r') n--; + if (buf[n - 1] == L'.') n--; + buf[n] = L'\0'; } - return n; + jstring s = (*env)->NewString(env, buf, n); + return s; } - - // C runtime error that has no corresponding DOS error code - if (errno == 0 || len < 1) return 0; - return strerror_s(buf, len, errno); + return NULL; } JNIEXPORT int JNICALL diff --git a/src/java.base/windows/native/libnio/ch/FileDispatcherImpl.c b/src/java.base/windows/native/libnio/ch/FileDispatcherImpl.c index fb0e61642a9..91effcf0bd0 100644 --- a/src/java.base/windows/native/libnio/ch/FileDispatcherImpl.c +++ b/src/java.base/windows/native/libnio/ch/FileDispatcherImpl.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -49,7 +49,7 @@ Java_sun_nio_ch_FileDispatcherImpl_read0(JNIEnv *env, jclass clazz, jobject fdo, HANDLE h = (HANDLE)(handleval(env, fdo)); if (h == INVALID_HANDLE_VALUE) { - JNU_ThrowIOExceptionWithLastError(env, "Invalid handle"); + JNU_ThrowIOException(env, "Invalid handle"); return IOS_THROWN; } result = ReadFile(h, /* File handle to read */ @@ -85,7 +85,7 @@ Java_sun_nio_ch_FileDispatcherImpl_readv0(JNIEnv *env, jclass clazz, jobject fdo HANDLE h = (HANDLE)(handleval(env, fdo)); if (h == INVALID_HANDLE_VALUE) { - JNU_ThrowIOExceptionWithLastError(env, "Invalid handle"); + JNU_ThrowIOException(env, "Invalid handle"); return IOS_THROWN; } @@ -131,7 +131,7 @@ Java_sun_nio_ch_FileDispatcherImpl_pread0(JNIEnv *env, jclass clazz, jobject fdo OVERLAPPED ov; if (h == INVALID_HANDLE_VALUE) { - JNU_ThrowIOExceptionWithLastError(env, "Invalid handle"); + JNU_ThrowIOException(env, "Invalid handle"); return IOS_THROWN; } @@ -199,9 +199,12 @@ Java_sun_nio_ch_FileDispatcherImpl_write0(JNIEnv *env, jclass clazz, jobject fdo len, /* number of bytes to write */ &written, /* receives number of bytes written */ lpOv); /* overlapped struct */ + } else { + JNU_ThrowIOException(env, "Invalid handle"); + return IOS_THROWN; } - if ((h == INVALID_HANDLE_VALUE) || (result == 0)) { + if (result == 0) { JNU_ThrowIOExceptionWithLastError(env, "Write failed"); return IOS_THROWN; } @@ -248,9 +251,12 @@ Java_sun_nio_ch_FileDispatcherImpl_writev0(JNIEnv *env, jclass clazz, jobject fd break; } } + } else { + JNU_ThrowIOException(env, "Invalid handle"); + return IOS_THROWN; } - if ((h == INVALID_HANDLE_VALUE) || (result == 0)) { + if (result == 0) { JNU_ThrowIOExceptionWithLastError(env, "Write failed"); return IOS_THROWN; } @@ -268,6 +274,10 @@ Java_sun_nio_ch_FileDispatcherImpl_pwrite0(JNIEnv *env, jclass clazz, jobject fd LARGE_INTEGER currPos; OVERLAPPED ov; + if (h == INVALID_HANDLE_VALUE) { + JNU_ThrowIOException(env, "Invalid handle"); + return IOS_THROWN; + } currPos.QuadPart = 0; result = SetFilePointerEx(h, currPos, &currPos, FILE_CURRENT); if (result == 0) { @@ -285,7 +295,7 @@ Java_sun_nio_ch_FileDispatcherImpl_pwrite0(JNIEnv *env, jclass clazz, jobject fd &written, /* receives number of bytes written */ &ov); /* position to write at */ - if ((h == INVALID_HANDLE_VALUE) || (result == 0)) { + if (result == 0) { JNU_ThrowIOExceptionWithLastError(env, "Write failed"); return IOS_THROWN; } @@ -341,7 +351,7 @@ Java_sun_nio_ch_FileDispatcherImpl_force0(JNIEnv *env, jobject this, } } } else { - JNU_ThrowIOExceptionWithLastError(env, "Force failed"); + JNU_ThrowIOException(env, "Invalid handle"); return IOS_THROWN; } return 0; diff --git a/src/jdk.hotspot.agent/share/native/libsaproc/sadis.c b/src/jdk.hotspot.agent/share/native/libsaproc/sadis.c index 846ea3b1b0a..62f6303c32b 100644 --- a/src/jdk.hotspot.agent/share/native/libsaproc/sadis.c +++ b/src/jdk.hotspot.agent/share/native/libsaproc/sadis.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -82,11 +82,9 @@ JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_asm_Disassembler_load_1library(JNIE jclass disclass, jstring libname_s) { uintptr_t func = 0; - const char *error_message = NULL; const char *libname = NULL; #ifdef _WINDOWS - char buffer[JVM_MAXPATHLEN]; HINSTANCE hsdis_handle = (HINSTANCE) NULL; #else void* hsdis_handle = NULL; @@ -104,8 +102,7 @@ JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_asm_Disassembler_load_1library(JNIE func = (uintptr_t)GetProcAddress(hsdis_handle, "decode_instructions_virtual"); } if (func == 0) { - getLastErrorString(buffer, sizeof(buffer)); - error_message = buffer; + JNU_ThrowByNameWithLastError(env, "sun/jvm/hotspot/debugger/DebuggerException", "GetProcAddress failed"); } #else hsdis_handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL); @@ -113,24 +110,11 @@ JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_asm_Disassembler_load_1library(JNIE func = (uintptr_t)dlsym(hsdis_handle, "decode_instructions_virtual"); } if (func == 0) { - error_message = dlerror(); + JNU_ThrowByName(env, "sun/jvm/hotspot/debugger/DebuggerException", dlerror()); } #endif (*env)->ReleaseStringUTFChars(env, libname_s, libname); - - if (func == 0) { - /* Couldn't find entry point. error_message should contain some - * platform dependent error message. - */ - jstring s = JNU_NewStringPlatform(env, error_message); - if (s != NULL) { - jobject x = JNU_NewObjectByName(env, "sun/jvm/hotspot/debugger/DebuggerException", "(Ljava/lang/String;)V", s); - if (x != NULL) { - (*env)->Throw(env, x); - } - } - } return (jlong)func; }