From cd9e241f0ec10c7b31d36cbfb994bc20d81a0517 Mon Sep 17 00:00:00 2001 From: Julian Waters Date: Tue, 27 Aug 2024 04:13:54 +0000 Subject: [PATCH] 8336289: Obliterate most references to _snprintf in the Windows JDK Reviewed-by: kbarrett, dholmes, jpai, mullan, djelinski, prr --- .../os/windows/attachListener_windows.cpp | 2 +- src/hotspot/os/windows/os_windows.cpp | 12 ++-- src/hotspot/os/windows/perfMemory_windows.cpp | 8 +-- .../windows/native/libjli/cmdtoargs.c | 4 +- .../libsplashscreen/splashscreen_impl.c | 7 +-- .../native/libawt/windows/awt_Debug.cpp | 2 +- .../jaccessinspector/jaccessinspector.cpp | 6 +- .../windows/native/libj2pkcs11/j2secmod_md.c | 4 +- .../share/native/libsaproc/sadis.c | 8 +-- .../windows/native/libjdwp/linker_md.c | 4 +- .../windows/native/libjdwp/util_md.h | 7 +-- .../libmanagement_ext/OperatingSystemImpl.c | 60 +++++++++---------- 12 files changed, 54 insertions(+), 70 deletions(-) diff --git a/src/hotspot/os/windows/attachListener_windows.cpp b/src/hotspot/os/windows/attachListener_windows.cpp index 7e455cf0a49..3f6ca941c20 100644 --- a/src/hotspot/os/windows/attachListener_windows.cpp +++ b/src/hotspot/os/windows/attachListener_windows.cpp @@ -318,7 +318,7 @@ void Win32AttachOperation::complete(jint result, bufferedStream* result_stream) BOOL fSuccess; char msg[32]; - _snprintf(msg, sizeof(msg), "%d\n", result); + os::snprintf(msg, sizeof(msg), "%d\n", result); msg[sizeof(msg) - 1] = '\0'; fSuccess = write_pipe(hPipe, msg, (int)strlen(msg)); diff --git a/src/hotspot/os/windows/os_windows.cpp b/src/hotspot/os/windows/os_windows.cpp index cb0fe95269b..1d1d9d3e1a4 100644 --- a/src/hotspot/os/windows/os_windows.cpp +++ b/src/hotspot/os/windows/os_windows.cpp @@ -1776,14 +1776,14 @@ void * os::dll_load(const char *name, char *ebuf, int ebuflen) { } if (lib_arch_str != nullptr) { - ::_snprintf(ebuf, ebuflen - 1, - "Can't load %s-bit .dll on a %s-bit platform", - lib_arch_str, running_arch_str); + os::snprintf(ebuf, ebuflen - 1, + "Can't load %s-bit .dll on a %s-bit platform", + lib_arch_str, running_arch_str); } else { // don't know what architecture this dll was build for - ::_snprintf(ebuf, ebuflen - 1, - "Can't load this .dll (machine code=0x%x) on a %s-bit platform", - lib_arch, running_arch_str); + os::snprintf(ebuf, ebuflen - 1, + "Can't load this .dll (machine code=0x%x) on a %s-bit platform", + lib_arch, running_arch_str); } JFR_ONLY(load_event.set_error_msg(ebuf);) return nullptr; diff --git a/src/hotspot/os/windows/perfMemory_windows.cpp b/src/hotspot/os/windows/perfMemory_windows.cpp index a71101731fb..06b057315cb 100644 --- a/src/hotspot/os/windows/perfMemory_windows.cpp +++ b/src/hotspot/os/windows/perfMemory_windows.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2024, 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 @@ -165,7 +165,7 @@ static char* get_user_tmp_dir(const char* user) { char* dirname = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); // construct the path name to user specific tmp directory - _snprintf(dirname, nbytes, "%s\\%s_%s", tmpdir, perfdir, user); + os::snprintf(dirname, nbytes, "%s\\%s_%s", tmpdir, perfdir, user); return dirname; } @@ -455,7 +455,7 @@ static char *get_sharedmem_objectname(const char* user, int vmid) { // nbytes += UINT_CHARS; char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); - _snprintf(name, nbytes, "%s_%s_%u", PERFDATA_NAME, user, vmid); + os::snprintf(name, nbytes, "%s_%s_%u", PERFDATA_NAME, user, vmid); return name; } @@ -471,7 +471,7 @@ static char* get_sharedmem_filename(const char* dirname, int vmid) { size_t nbytes = strlen(dirname) + UINT_CHARS + 2; char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); - _snprintf(name, nbytes, "%s\\%d", dirname, vmid); + os::snprintf(name, nbytes, "%s\\%d", dirname, vmid); return name; } diff --git a/src/java.base/windows/native/libjli/cmdtoargs.c b/src/java.base/windows/native/libjli/cmdtoargs.c index 548e70b9bbf..27a28b4cd45 100644 --- a/src/java.base/windows/native/libjli/cmdtoargs.c +++ b/src/java.base/windows/native/libjli/cmdtoargs.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024, 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 @@ -328,7 +328,7 @@ public: printf("*** cannot allocate memory\n"); doabort(); } - _snprintf(cptr, MAX_PATH, "\"%s\" %s", argv[0], cmdline); + snprintf(cptr, MAX_PATH, "\"%s\" %s", argv[0], cmdline); JLI_CmdToArgs(cptr); free(cptr); StdArg *kargv = JLI_GetStdArgs(); diff --git a/src/java.desktop/share/native/libsplashscreen/splashscreen_impl.c b/src/java.desktop/share/native/libsplashscreen/splashscreen_impl.c index 5891a66d6ac..05ef8c33a83 100644 --- a/src/java.desktop/share/native/libsplashscreen/splashscreen_impl.c +++ b/src/java.desktop/share/native/libsplashscreen/splashscreen_impl.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2024, 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 @@ -26,11 +26,6 @@ #include "splashscreen_impl.h" #include "splashscreen_gfx_impl.h" #define BUFF_SIZE 1024 -#ifdef _MSC_VER -# ifndef snprintf -# define snprintf _snprintf -# endif -#endif int splashIsVisible = 0; Splash * diff --git a/src/java.desktop/windows/native/libawt/windows/awt_Debug.cpp b/src/java.desktop/windows/native/libawt/windows/awt_Debug.cpp index 9e4b1ff1bf0..3b8043385a1 100644 --- a/src/java.desktop/windows/native/libawt/windows/awt_Debug.cpp +++ b/src/java.desktop/windows/native/libawt/windows/awt_Debug.cpp @@ -141,7 +141,7 @@ void AwtDebugSupport::AssertCallback(const char * expr, const char * file, int l msgBuffer = (LPSTR)""; } // format the assertion message - _snprintf(assertMsg, ASSERT_MSG_SIZE, AssertFmt, expr, file, line, lastError, msgBuffer); + snprintf(assertMsg, ASSERT_MSG_SIZE, AssertFmt, expr, file, line, lastError, msgBuffer); if (fret != 0) { LocalFree(msgBuffer); } diff --git a/src/jdk.accessibility/windows/native/jaccessinspector/jaccessinspector.cpp b/src/jdk.accessibility/windows/native/jaccessinspector/jaccessinspector.cpp index 22992d481ed..38e093eda09 100644 --- a/src/jdk.accessibility/windows/native/jaccessinspector/jaccessinspector.cpp +++ b/src/jdk.accessibility/windows/native/jaccessinspector/jaccessinspector.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2024, 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 @@ -1526,8 +1526,8 @@ BOOL UpdateMessageNumber () { size_t messageNumber = g_MessageHistory.GetCurrentMessageIndex() + 1; char text [32] = {0}; if ( 0 != messageCount ) { - ::_snprintf(text, sizeof(text), "%d of %d", (int)messageNumber, - (int) messageCount); + ::snprintf(text, sizeof(text), "%d of %d", (int)messageNumber, + (int) messageCount); } return ::SetWindowText(dlgItem, text); } diff --git a/src/jdk.crypto.cryptoki/windows/native/libj2pkcs11/j2secmod_md.c b/src/jdk.crypto.cryptoki/windows/native/libj2pkcs11/j2secmod_md.c index 305d202030b..a5cd5e63d02 100644 --- a/src/jdk.crypto.cryptoki/windows/native/libj2pkcs11/j2secmod_md.c +++ b/src/jdk.crypto.cryptoki/windows/native/libj2pkcs11/j2secmod_md.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2024, 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 @@ -39,7 +39,7 @@ void *p11FindFunction(JNIEnv *env, jlong jHandle, const char *functionName) { void *fAddress = GetProcAddress(hModule, functionName); if (fAddress == NULL) { char errorMessage[256]; - _snprintf(errorMessage, sizeof(errorMessage), "Symbol not found: %s", functionName); + snprintf(errorMessage, sizeof(errorMessage), "Symbol not found: %s", functionName); p11ThrowNullPointerException(env, errorMessage); return NULL; } diff --git a/src/jdk.hotspot.agent/share/native/libsaproc/sadis.c b/src/jdk.hotspot.agent/share/native/libsaproc/sadis.c index 62f6303c32b..cf66b7184e3 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, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024, 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 @@ -33,12 +33,6 @@ */ #ifdef _WINDOWS -// Disable CRT security warning against _snprintf -#pragma warning (disable : 4996) - -#define snprintf _snprintf -#define vsnprintf _vsnprintf - #include #include #include diff --git a/src/jdk.jdwp.agent/windows/native/libjdwp/linker_md.c b/src/jdk.jdwp.agent/windows/native/libjdwp/linker_md.c index 97eb1498f58..a2cc952db81 100644 --- a/src/jdk.jdwp.agent/windows/native/libjdwp/linker_md.c +++ b/src/jdk.jdwp.agent/windows/native/libjdwp/linker_md.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2024, 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 @@ -56,7 +56,7 @@ static void dll_build_name(char* buffer, size_t buflen, path = strtok_s(paths_copy, PATH_SEPARATOR, &next_token); while (path != NULL) { - size_t result_len = (size_t)_snprintf(buffer, buflen, "%s\\%s.dll", path, fname); + size_t result_len = (size_t) snprintf(buffer, buflen, "%s\\%s.dll", path, fname); if (result_len >= buflen) { EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, " "likely by sun.boot.library.path, is too long."); diff --git a/src/jdk.jdwp.agent/windows/native/libjdwp/util_md.h b/src/jdk.jdwp.agent/windows/native/libjdwp/util_md.h index 4af5aced114..cefb301e095 100644 --- a/src/jdk.jdwp.agent/windows/native/libjdwp/util_md.h +++ b/src/jdk.jdwp.agent/windows/native/libjdwp/util_md.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2024, 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 @@ -34,11 +34,6 @@ typedef unsigned long UNSIGNED_JINT; #define MAXPATHLEN _MAX_PATH -/* Needed on Windows because names seem to be hidden in stdio.h. */ - -#define snprintf _snprintf -#define vsnprintf _vsnprintf - /* On little endian machines, convert java big endian numbers. */ #define HOST_TO_JAVA_CHAR(x) (((x & 0xff) << 8) | ((x >> 8) & (0xff))) diff --git a/src/jdk.management/windows/native/libmanagement_ext/OperatingSystemImpl.c b/src/jdk.management/windows/native/libmanagement_ext/OperatingSystemImpl.c index 746bd97c7ff..3cfd4679134 100644 --- a/src/jdk.management/windows/native/libmanagement_ext/OperatingSystemImpl.c +++ b/src/jdk.management/windows/native/libmanagement_ext/OperatingSystemImpl.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2024, 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 @@ -436,13 +436,13 @@ makeFullCounterPath(const char* const objectName, return NULL; } - _snprintf(fullCounterPath, - fullCounterPathLen, - PROCESS_OBJECT_INSTANCE_COUNTER_FMT, - objectName, - imageName, - instance, - counterName); + snprintf(fullCounterPath, + fullCounterPathLen, + PROCESS_OBJECT_INSTANCE_COUNTER_FMT, + objectName, + imageName, + instance, + counterName); } else { if (instance) { /* @@ -472,18 +472,18 @@ makeFullCounterPath(const char* const objectName, } if (instance) { - _snprintf(fullCounterPath, - fullCounterPathLen, - OBJECT_WITH_INSTANCES_COUNTER_FMT, - objectName, - instance, - counterName); + snprintf(fullCounterPath, + fullCounterPathLen, + OBJECT_WITH_INSTANCES_COUNTER_FMT, + objectName, + instance, + counterName); } else { - _snprintf(fullCounterPath, - fullCounterPathLen, - OBJECT_COUNTER_FMT, - objectName, - counterName); + snprintf(fullCounterPath, + fullCounterPathLen, + OBJECT_COUNTER_FMT, + objectName, + counterName); } } @@ -719,10 +719,10 @@ currentQueryIndexForProcess(void) { PDH_FMT_COUNTERVALUE counterValue; PDH_STATUS res; - _snprintf(fullIDProcessCounterPath, - MAX_PATH, - pdhIDProcessCounterFmt, - index); + snprintf(fullIDProcessCounterPath, + MAX_PATH, + pdhIDProcessCounterFmt, + index); if (addCounter(tmpQuery, fullIDProcessCounterPath, &handleCounter) != 0) { break; @@ -1059,13 +1059,13 @@ allocateAndInitializePdhConstants() { } /* "\Process(java#%d)\ID Process" */ - _snprintf(pdhIDProcessCounterFmt, - pdhIDProcessCounterFmtLen, - PROCESS_OBJECT_INSTANCE_COUNTER_FMT, - pdhLocalizedProcessObject, - pdhProcessImageName, - "%d", - pdhLocalizedIDProcessCounter); + snprintf(pdhIDProcessCounterFmt, + pdhIDProcessCounterFmtLen, + PROCESS_OBJECT_INSTANCE_COUNTER_FMT, + pdhLocalizedProcessObject, + pdhProcessImageName, + "%d", + pdhLocalizedIDProcessCounter); pdhIDProcessCounterFmt[pdhIDProcessCounterFmtLen] = '\0';