Skip to content

Commit

Permalink
8336289: Obliterate most references to _snprintf in the Windows JDK
Browse files Browse the repository at this point in the history
Reviewed-by: kbarrett, dholmes, jpai, mullan, djelinski, prr
  • Loading branch information
TheShermanTanker committed Aug 27, 2024
1 parent 78f53ef commit cd9e241
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/hotspot/os/windows/attachListener_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
12 changes: 6 additions & 6 deletions src/hotspot/os/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/hotspot/os/windows/perfMemory_windows.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/windows/native/libjli/cmdtoargs.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -328,7 +328,7 @@ class Vector {
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void AwtDebugSupport::AssertCallback(const char * expr, const char * file, int l
msgBuffer = (LPSTR)"<Could not get GetLastError() message text>";
}
// 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 1 addition & 7 deletions src/jdk.hotspot.agent/share/native/libsaproc/sadis.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -33,12 +33,6 @@
*/

#ifdef _WINDOWS
// Disable CRT security warning against _snprintf
#pragma warning (disable : 4996)

#define snprintf _snprintf
#define vsnprintf _vsnprintf

#include <windows.h>
#include <sys/types.h>
#include <sys/stat.h>
Expand Down
4 changes: 2 additions & 2 deletions src/jdk.jdwp.agent/windows/native/libjdwp/linker_md.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.");
Expand Down
7 changes: 1 addition & 6 deletions src/jdk.jdwp.agent/windows/native/libjdwp/util_md.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
/*
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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';

Expand Down

0 comments on commit cd9e241

Please sign in to comment.