--- old/make/lib/CoreLibraries.gmk Mon Sep 21 16:48:34 2015 +++ new/make/lib/CoreLibraries.gmk Mon Sep 21 16:48:33 2015 @@ -158,7 +158,8 @@ -framework CoreFoundation \ -framework Foundation \ -framework Security -framework SystemConfiguration, \ - LDFLAGS_SUFFIX_windows := -export:winFileHandleOpen -export:handleLseek \ + LDFLAGS_SUFFIX_windows := -export:jdk_strerror \ + -export:winFileHandleOpen -export:handleLseek \ -export:getLastErrorString \ jvm.lib $(BUILD_LIBFDLIBM) $(WIN_VERIFY_LIB) \ shell32.lib delayimp.lib -DELAYLOAD:shell32.dll \ @@ -347,6 +348,12 @@ ) endif +LIBJLI_CFLAGS += -I$(JDK_TOPDIR)/src/java.base/share/native/libjava +LIBJLI_EXTRA_FILES += \ + $(addprefix $(JDK_TOPDIR)/src/java.base/share/native/libjava/, \ + jdk_strerror.c \ + ) + $(eval $(call SetupNativeCompilation,BUILD_LIBJLI, \ LIBRARY := jli, \ OUTPUT_DIR := $(LIBJLI_OUTPUT_DIR), \ --- old/make/mapfiles/libjava/mapfile-vers Mon Sep 21 16:48:35 2015 +++ new/make/mapfiles/libjava/mapfile-vers Mon Sep 21 16:48:34 2015 @@ -73,6 +73,8 @@ JNU_ThrowStringIndexOutOfBoundsException; JNU_ToString; + jdk_strerror; + Java_java_io_FileDescriptor_initIDs; Java_java_io_FileDescriptor_sync; Java_java_io_FileDescriptor_getAppend; --- old/src/java.base/share/native/libzip/zip_util.c Mon Sep 21 16:48:36 2015 +++ new/src/java.base/share/native/libzip/zip_util.c Mon Sep 21 16:48:35 2015 @@ -37,6 +37,7 @@ #include #include +#include "jdk_strerror.h" #include "jni.h" #include "jni_util.h" #include "jlong.h" @@ -1438,6 +1439,7 @@ ZIP_ReadEntry(jzfile *zip, jzentry *entry, unsigned char *buf, char *entryname) { char *msg; + char tmpbuf[1024]; strcpy(entryname, entry->name); if (entry->csize == 0) { @@ -1456,8 +1458,11 @@ msg = zip->msg; ZIP_Unlock(zip); if (n == -1) { - jio_fprintf(stderr, "%s: %s\n", zip->name, - msg != 0 ? msg : strerror(errno)); + if (msg == 0) { + jdk_strerror(errno, tmpbuf, (size_t) 1024); + msg = tmpbuf; + } + jio_fprintf(stderr, "%s: %s\n", zip->name, msg); return JNI_FALSE; } buf += n; @@ -1470,8 +1475,11 @@ if ((msg == NULL) || (*msg == 0)) { msg = zip->msg; } - jio_fprintf(stderr, "%s: %s\n", zip->name, - msg != 0 ? msg : strerror(errno)); + if (msg == 0) { + jdk_strerror(errno, tmpbuf, (size_t) 1024); + msg = tmpbuf; + } + jio_fprintf(stderr, "%s: %s\n", zip->name, msg); return JNI_FALSE; } } --- old/src/java.base/unix/native/libjava/ProcessImpl_md.c Mon Sep 21 16:48:37 2015 +++ new/src/java.base/unix/native/libjava/ProcessImpl_md.c Mon Sep 21 16:48:37 2015 @@ -26,6 +26,7 @@ #undef _LARGEFILE64_SOURCE #define _LARGEFILE64_SOURCE 1 +#include "jdk_strerror.h" #include "jni.h" #include "jvm.h" #include "jvm_md.h" @@ -248,12 +249,13 @@ const char *detail = defaultDetail; char *errmsg; size_t fmtsize; + char buf[1024]; jstring s; if (errnum != 0) { - const char *s = strerror(errnum); - if (strcmp(s, "Unknown error") != 0) - detail = s; + jdk_strerror(errnum, buf, (size_t) 1024); + if (strncmp(buf, "Unknown error", 13) != 0) + detail = buf; } /* ASCII Decimal representation uses 2.4 times as many bits as binary. */ fmtsize = sizeof(IOE_FORMAT) + strlen(detail) + 3 * sizeof(errnum); --- old/src/java.base/unix/native/libjava/jni_util_md.c Mon Sep 21 16:48:38 2015 +++ new/src/java.base/unix/native/libjava/jni_util_md.c Mon Sep 21 16:48:38 2015 @@ -26,6 +26,7 @@ #include #include +#include "jdk_strerror.h" #include "jni.h" #include "jni_util.h" #include "dlfcn.h" @@ -55,16 +56,17 @@ size_t getLastErrorString(char *buf, size_t len) { - char *err; - size_t n; + char tmpbuf[1024]; + int n = 0; + if (errno == 0 || len < 1) return 0; - err = strerror(errno); - n = strlen(err); - if (n >= len) - n = len - 1; + jdk_strerror(errno, tmpbuf, (size_t) 1024); + n = strlen(tmpbuf); + if (n >= (int) len) + n = (int) len - 1; - strncpy(buf, err, n); + strncpy(buf, tmpbuf, n); buf[n] = '\0'; return n; } --- old/src/java.base/unix/native/libjli/java_md_common.c Mon Sep 21 16:48:39 2015 +++ new/src/java.base/unix/native/libjli/java_md_common.c Mon Sep 21 16:48:39 2015 @@ -23,6 +23,7 @@ * questions. */ #include "java.h" +// #include "jdk_strerror.h" /* * If app is "/foo/bin/javac", or "/foo/bin/sparcv9/javac" then put @@ -146,16 +147,10 @@ void JLI_ReportErrorMessageSys(const char* fmt, ...) { va_list vl; - char *emsg; + char buf[1024]; - /* - * TODO: its safer to use strerror_r but is not available on - * Solaris 8. Until then.... - */ - emsg = strerror(errno); - if (emsg != NULL) { - fprintf(stderr, "%s\n", emsg); - } + // jdk_strerror(errno, buf, (size_t) 1024) + fprintf(stderr, "%s\n", strerror(errno)); va_start(vl, fmt); vfprintf(stderr, fmt, vl); --- old/src/java.base/unix/native/libnet/PlainDatagramSocketImpl.c Mon Sep 21 16:48:40 2015 +++ new/src/java.base/unix/native/libnet/PlainDatagramSocketImpl.c Mon Sep 21 16:48:40 2015 @@ -68,6 +68,7 @@ #endif #include "jvm.h" +#include "jdk_strerror.h" #include "jni_util.h" #include "net_util.h" #include "java_net_SocketOptions.h" --- old/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c Mon Sep 21 16:48:41 2015 +++ new/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c Mon Sep 21 16:48:41 2015 @@ -66,6 +66,7 @@ #define readdir64_r readdir_r #endif +#include "jdk_strerror.h" #include "jni.h" #include "jni_util.h" #include "jlong.h" @@ -315,21 +316,15 @@ JNIEXPORT jbyteArray Java_sun_nio_fs_UnixNativeDispatcher_strerror(JNIEnv* env, jclass this, jint error) { - char* msg; + char buf[1024]; jsize len; jbyteArray bytes; -#ifdef _AIX - /* strerror() is not thread-safe on AIX so we have to use strerror_r() */ - char buffer[256]; - msg = (strerror_r((int)error, buffer, 256) == 0) ? buffer : "Error while calling strerror_r"; -#else - msg = strerror((int)error); -#endif - len = strlen(msg); + jdk_strerror((int)error, buf, (size_t) 1024); + len = strlen(buf); bytes = (*env)->NewByteArray(env, len); if (bytes != NULL) { - (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)msg); + (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)buf); } return bytes; } --- old/src/java.base/windows/native/libjli/java_md.c Mon Sep 21 16:48:43 2015 +++ new/src/java.base/windows/native/libjli/java_md.c Mon Sep 21 16:48:42 2015 @@ -37,6 +37,7 @@ #include #include "java.h" +#include "jdk_strerror.h" #define JVM_DLL "jvm.dll" #define JAVA_DLL "java.dll" @@ -521,6 +522,7 @@ DWORD errval; jboolean freeit = JNI_FALSE; char *errtext = NULL; + char buf[1024]; va_start(vl, fmt); @@ -540,7 +542,8 @@ } } } else { /* C runtime error that has no corresponding DOS error code */ - errtext = strerror(save_errno); + jdk_strerror(save_errno, buf, (size_t) 1024); + errtext = buf; } if (IsJavaw()) { --- old/src/java.base/windows/native/libnet/TwoStacksPlainDatagramSocketImpl.c Mon Sep 21 16:48:44 2015 +++ new/src/java.base/windows/native/libnet/TwoStacksPlainDatagramSocketImpl.c Mon Sep 21 16:48:43 2015 @@ -45,6 +45,7 @@ #include "NetworkInterface.h" #include "jvm.h" +#include "jdk_strerror.h" #include "jni_util.h" #include "net_util.h" @@ -2168,6 +2169,7 @@ int fd=-1, fd1=-1; int level, optname, optlen; + char buf[1024]; union { int i; } optval = {0}; @@ -2211,8 +2213,10 @@ optlen = sizeof(optval.i); if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) { - char errmsg[255]; - sprintf(errmsg, "error getting socket option: %s\n", strerror(errno)); + int size = 0; + char errmsg[300]; + jdk_strerror(errno, buf, (size_t) 255); + sprintf(errmsg, "error getting socket option: %s\n", buf); JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg); return NULL; } --- /dev/null Mon Sep 21 16:48:45 2015 +++ new/src/java.base/share/native/libjava/jdk_strerror.c Mon Sep 21 16:48:45 2015 @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2015, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +#include +#include +#include +#include + +#include "jdk_strerror.h" + +int +jdk_strerror(int err, char *buf, size_t len) +{ + int ret = 0; + + if (err == 0 || len < 1) return 0; + +#if defined(__linux__) && (defined(_GNU_SOURCE) || \ + (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE < 200112L \ + && defined(_XOPEN_SOURCE) && _XOPEN_SOURCE < 600)) + + const char *msg; + char tmpbuf[1024]; + msg = strerror_r (errno, tmpbuf, (size_t) 1024); + + size_t n = strlen(msg); + if (n >= len) { + n = len - 1; + ret = ERANGE; + } + + strncpy(buf, msg, n); + buf[n] = '\0'; + + if (strncmp(msg, "Unknown error", 13) == 0) + ret = EINVAL; + +#elif _WIN32 + ret = strerror_s(buf, len, err); +#else + ret = strerror_r (err, buf, len); +#endif + + return ret; +} --- /dev/null Mon Sep 21 16:48:46 2015 +++ new/src/java.base/share/native/libjava/jdk_strerror.h Mon Sep 21 16:48:46 2015 @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2015, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +#ifndef JDK_STRERROR_H +#define JDK_STRERROR_H + +#include + +int +jdk_strerror(int err, char *buf, size_t len); + +#endif /* JDK_STRERROR_H */