Skip to content
4 changes: 2 additions & 2 deletions src/hotspot/cpu/aarch64/frame_aarch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,10 @@ static void printbc(Method *m, intptr_t bcx) {
if (m->validate_bci_from_bcp((address)bcx) < 0
|| !m->contains((address)bcx)) {
name = "???";
snprintf(buf, sizeof buf, "(bad)");
os::snprintf_checked(buf, sizeof buf, "(bad)");
} else {
int bci = m->bci_from((address)bcx);
snprintf(buf, sizeof buf, "%d", bci);
os::snprintf_checked(buf, sizeof buf, "%d", bci);
name = Bytecodes::name(m->code_at(bci));
}
ResourceMark rm;
Expand Down
24 changes: 12 additions & 12 deletions src/hotspot/cpu/aarch64/macroAssembler_aarch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2259,7 +2259,7 @@ void MacroAssembler::movptr(Register r, uintptr_t imm64) {
#ifndef PRODUCT
{
char buffer[64];
snprintf(buffer, sizeof(buffer), "0x%" PRIX64, (uint64_t)imm64);
os::snprintf_checked(buffer, sizeof(buffer), "0x%" PRIX64, (uint64_t)imm64);
block_comment(buffer);
}
#endif
Expand Down Expand Up @@ -2317,7 +2317,7 @@ void MacroAssembler::mov_immediate64(Register dst, uint64_t imm64)
#ifndef PRODUCT
{
char buffer[64];
snprintf(buffer, sizeof(buffer), "0x%" PRIX64, imm64);
os::snprintf_checked(buffer, sizeof(buffer), "0x%" PRIX64, imm64);
block_comment(buffer);
}
#endif
Expand Down Expand Up @@ -2430,7 +2430,7 @@ void MacroAssembler::mov_immediate32(Register dst, uint32_t imm32)
#ifndef PRODUCT
{
char buffer[64];
snprintf(buffer, sizeof(buffer), "0x%" PRIX32, imm32);
os::snprintf_checked(buffer, sizeof(buffer), "0x%" PRIX32, imm32);
block_comment(buffer);
}
#endif
Expand Down Expand Up @@ -2902,11 +2902,11 @@ int MacroAssembler::push_fp(unsigned int bitset, Register stack, FpPushPopMode m
{
char buffer[48];
if (mode == PushPopSVE) {
snprintf(buffer, sizeof(buffer), "push_fp: %d SVE registers", count);
os::snprintf_checked(buffer, sizeof(buffer), "push_fp: %d SVE registers", count);
} else if (mode == PushPopNeon) {
snprintf(buffer, sizeof(buffer), "push_fp: %d Neon registers", count);
os::snprintf_checked(buffer, sizeof(buffer), "push_fp: %d Neon registers", count);
} else {
snprintf(buffer, sizeof(buffer), "push_fp: %d fp registers", count);
os::snprintf_checked(buffer, sizeof(buffer), "push_fp: %d fp registers", count);
}
block_comment(buffer);
}
Expand Down Expand Up @@ -3014,11 +3014,11 @@ int MacroAssembler::pop_fp(unsigned int bitset, Register stack, FpPushPopMode mo
{
char buffer[48];
if (mode == PushPopSVE) {
snprintf(buffer, sizeof(buffer), "pop_fp: %d SVE registers", count);
os::snprintf_checked(buffer, sizeof(buffer), "pop_fp: %d SVE registers", count);
} else if (mode == PushPopNeon) {
snprintf(buffer, sizeof(buffer), "pop_fp: %d Neon registers", count);
os::snprintf_checked(buffer, sizeof(buffer), "pop_fp: %d Neon registers", count);
} else {
snprintf(buffer, sizeof(buffer), "pop_fp: %d fp registers", count);
os::snprintf_checked(buffer, sizeof(buffer), "pop_fp: %d fp registers", count);
}
block_comment(buffer);
}
Expand Down Expand Up @@ -5920,7 +5920,7 @@ address MacroAssembler::arrays_equals(Register a1, Register a2, Register tmp3,
{
const char kind = (elem_size == 2) ? 'U' : 'L';
char comment[64];
snprintf(comment, sizeof comment, "array_equals%c{", kind);
os::snprintf_checked(comment, sizeof comment, "array_equals%c{", kind);
BLOCK_COMMENT(comment);
}
#endif
Expand Down Expand Up @@ -6118,7 +6118,7 @@ void MacroAssembler::string_equals(Register a1, Register a2,
#ifndef PRODUCT
{
char comment[64];
snprintf(comment, sizeof comment, "{string_equalsL");
os::snprintf_checked(comment, sizeof comment, "{string_equalsL");
BLOCK_COMMENT(comment);
}
#endif
Expand Down Expand Up @@ -6266,7 +6266,7 @@ address MacroAssembler::zero_words(Register base, uint64_t cnt)
#ifndef PRODUCT
{
char buf[64];
snprintf(buf, sizeof buf, "zero_words (count = %" PRIu64 ") {", cnt);
os::snprintf_checked(buf, sizeof buf, "zero_words (count = %" PRIu64 ") {", cnt);
BLOCK_COMMENT(buf);
}
#endif
Expand Down
6 changes: 3 additions & 3 deletions src/hotspot/cpu/aarch64/vm_version_aarch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,12 +721,12 @@ void VM_Version::initialize_cpu_information(void) {
_no_of_cores = os::processor_count();
_no_of_threads = _no_of_cores;
_no_of_sockets = _no_of_cores;
snprintf(_cpu_name, CPU_TYPE_DESC_BUF_SIZE - 1, "AArch64");
os::snprintf_checked(_cpu_name, CPU_TYPE_DESC_BUF_SIZE - 1, "AArch64");

int desc_len = snprintf(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "AArch64 ");
int desc_len = os::snprintf(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "AArch64 ");
get_compatible_board(_cpu_desc + desc_len, CPU_DETAILED_DESC_BUF_SIZE - desc_len);
desc_len = (int)strlen(_cpu_desc);
snprintf(_cpu_desc + desc_len, CPU_DETAILED_DESC_BUF_SIZE - desc_len, " %s", _cpu_info_string);
os::snprintf_checked(_cpu_desc + desc_len, CPU_DETAILED_DESC_BUF_SIZE - desc_len, " %s", _cpu_info_string);

_initialized = true;
}
2 changes: 1 addition & 1 deletion src/hotspot/cpu/arm/macroAssembler_arm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ void MacroAssembler::_verify_oop(Register reg, const char* s, const char* file,
char buffer[64];
#ifdef COMPILER1
if (CommentedAssembly) {
snprintf(buffer, sizeof(buffer), "verify_oop at %d", offset());
os::snprintf_checked(buffer, sizeof(buffer), "verify_oop at %d", offset());
block_comment(buffer);
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/cpu/arm/vm_version_arm_32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ void VM_Version::initialize_cpu_information(void) {
_no_of_cores = os::processor_count();
_no_of_threads = _no_of_cores;
_no_of_sockets = _no_of_cores;
snprintf(_cpu_name, CPU_TYPE_DESC_BUF_SIZE - 1, "ARM%d", _arm_arch);
snprintf(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "%s", _cpu_info_string);
os::snprintf_checked(_cpu_name, CPU_TYPE_DESC_BUF_SIZE - 1, "ARM%d", _arm_arch);
os::snprintf_checked(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "%s", _cpu_info_string);
_initialized = true;
}
4 changes: 2 additions & 2 deletions src/hotspot/cpu/ppc/vm_version_ppc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ void VM_Version::initialize_cpu_information(void) {
_no_of_cores = os::processor_count();
_no_of_threads = _no_of_cores;
_no_of_sockets = _no_of_cores;
snprintf(_cpu_name, CPU_TYPE_DESC_BUF_SIZE, "PowerPC POWER%lu", PowerArchitecturePPC64);
snprintf(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "PPC %s", cpu_info_string());
os::snprintf_checked(_cpu_name, CPU_TYPE_DESC_BUF_SIZE, "PowerPC POWER%lu", PowerArchitecturePPC64);
os::snprintf_checked(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "PPC %s", cpu_info_string());
_initialized = true;
}
2 changes: 1 addition & 1 deletion src/hotspot/cpu/riscv/macroAssembler_riscv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2679,7 +2679,7 @@ void MacroAssembler::movptr(Register Rd, address addr, int32_t &offset, Register
#ifndef PRODUCT
{
char buffer[64];
snprintf(buffer, sizeof(buffer), "0x%" PRIx64, uimm64);
os::snprintf_checked(buffer, sizeof(buffer), "0x%" PRIx64, uimm64);
block_comment(buffer);
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/cpu/riscv/vm_version_riscv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,8 @@ void VM_Version::initialize_cpu_information(void) {
_no_of_cores = os::processor_count();
_no_of_threads = _no_of_cores;
_no_of_sockets = _no_of_cores;
snprintf(_cpu_name, CPU_TYPE_DESC_BUF_SIZE - 1, "RISCV64");
snprintf(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "RISCV64 %s", cpu_info_string());
os::snprintf_checked(_cpu_name, CPU_TYPE_DESC_BUF_SIZE - 1, "RISCV64");
os::snprintf_checked(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "RISCV64 %s", cpu_info_string());
_initialized = true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/cpu/s390/vm_version_s390.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,7 @@ void VM_Version::initialize_cpu_information(void) {
_no_of_cores = os::processor_count();
_no_of_threads = _no_of_cores;
_no_of_sockets = _no_of_cores;
snprintf(_cpu_name, CPU_TYPE_DESC_BUF_SIZE, "s390 %s", VM_Version::get_model_string());
snprintf(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "s390 %s", cpu_info_string());
os::snprintf_checked(_cpu_name, CPU_TYPE_DESC_BUF_SIZE, "s390 %s", VM_Version::get_model_string());
os::snprintf_checked(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "s390 %s", cpu_info_string());
_initialized = true;
}
4 changes: 2 additions & 2 deletions src/hotspot/cpu/x86/stubGenerator_x86_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4331,10 +4331,10 @@ void StubGenerator::generate_compiler_stubs() {
if (libsimdsort != nullptr) {
log_info(library)("Loaded library %s, handle " INTPTR_FORMAT, JNI_LIB_PREFIX "simdsort" JNI_LIB_SUFFIX, p2i(libsimdsort));

snprintf(ebuf_, sizeof(ebuf_), VM_Version::supports_avx512_simd_sort() ? "avx512_sort" : "avx2_sort");
os::snprintf_checked(ebuf_, sizeof(ebuf_), VM_Version::supports_avx512_simd_sort() ? "avx512_sort" : "avx2_sort");
StubRoutines::_array_sort = (address)os::dll_lookup(libsimdsort, ebuf_);

snprintf(ebuf_, sizeof(ebuf_), VM_Version::supports_avx512_simd_sort() ? "avx512_partition" : "avx2_partition");
os::snprintf_checked(ebuf_, sizeof(ebuf_), VM_Version::supports_avx512_simd_sort() ? "avx512_partition" : "avx2_partition");
StubRoutines::_array_partition = (address)os::dll_lookup(libsimdsort, ebuf_);
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/hotspot/cpu/zero/frame_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ void frame::zero_print_on_error(int frame_index,
int offset = fp() - addr;

// Fill in default values, then try and improve them
snprintf(fieldbuf, buflen, "word[%d]", offset);
snprintf(valuebuf, buflen, PTR_FORMAT, *addr);
os::snprintf_checked(fieldbuf, buflen, "word[%d]", offset);
os::snprintf_checked(valuebuf, buflen, PTR_FORMAT, *addr);
zeroframe()->identify_word(frame_index, offset, fieldbuf, valuebuf, buflen);
fieldbuf[buflen - 1] = '\0';
valuebuf[buflen - 1] = '\0';
Expand Down Expand Up @@ -300,7 +300,7 @@ void EntryFrame::identify_word(int frame_index,
break;

default:
snprintf(fieldbuf, buflen, "local[%d]", offset - 3);
os::snprintf_checked(fieldbuf, buflen, "local[%d]", offset - 3);
}
}

Expand All @@ -321,12 +321,12 @@ void InterpreterFrame::identify_word(int frame_index,
istate->method()->name_and_sig_as_C_string(valuebuf, buflen);
}
else if (is_valid && !strcmp(field, "_bcp") && istate->bcp()) {
snprintf(valuebuf, buflen, PTR_FORMAT " (bci %d)",
(intptr_t) istate->bcp(),
istate->method()->bci_from(istate->bcp()));
os::snprintf_checked(valuebuf, buflen, PTR_FORMAT " (bci %d)",
(intptr_t) istate->bcp(),
istate->method()->bci_from(istate->bcp()));
}
snprintf(fieldbuf, buflen, "%sistate->%s",
field[strlen(field) - 1] == ')' ? "(": "", field);
os::snprintf_checked(fieldbuf, buflen, "%sistate->%s",
field[strlen(field) - 1] == ')' ? "(": "", field);
}
else if (addr == (intptr_t *) istate) {
strncpy(fieldbuf, "(vtable for istate)", buflen);
Expand Down Expand Up @@ -358,13 +358,13 @@ void InterpreterFrame::identify_word(int frame_index,
else
desc = " (this)";
}
snprintf(fieldbuf, buflen, "parameter[%d]%s", param, desc);
os::snprintf_checked(fieldbuf, buflen, "parameter[%d]%s", param, desc);
return;
}

for (int i = 0; i < handler->argument_count(); i++) {
if (params[i] == (intptr_t) addr) {
snprintf(fieldbuf, buflen, "unboxed parameter[%d]", i);
os::snprintf_checked(fieldbuf, buflen, "unboxed parameter[%d]", i);
return;
}
}
Expand Down Expand Up @@ -396,18 +396,18 @@ void ZeroFrame::identify_vp_word(int frame_index,
intptr_t offset = (intptr_t) addr - monitor;

if (offset == in_bytes(BasicObjectLock::obj_offset()))
snprintf(fieldbuf, buflen, "monitor[%d]->_obj", index);
os::snprintf_checked(fieldbuf, buflen, "monitor[%d]->_obj", index);
else if (offset == in_bytes(BasicObjectLock::lock_offset()))
snprintf(fieldbuf, buflen, "monitor[%d]->_lock", index);
os::snprintf_checked(fieldbuf, buflen, "monitor[%d]->_lock", index);

return;
}

// Expression stack
if (addr < stack_base) {
snprintf(fieldbuf, buflen, "%s[%d]",
frame_index == 0 ? "stack_word" : "local",
(int) (stack_base - addr - 1));
os::snprintf_checked(fieldbuf, buflen, "%s[%d]",
frame_index == 0 ? "stack_word" : "local",
(int) (stack_base - addr - 1));
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/cpu/zero/vm_version_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void VM_Version::initialize_cpu_information(void) {
_no_of_cores = os::processor_count();
_no_of_threads = _no_of_cores;
_no_of_sockets = _no_of_cores;
snprintf(_cpu_name, CPU_TYPE_DESC_BUF_SIZE - 1, "Zero VM");
snprintf(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "%s", _cpu_info_string);
os::snprintf_checked(_cpu_name, CPU_TYPE_DESC_BUF_SIZE - 1, "Zero VM");
os::snprintf_checked(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "%s", _cpu_info_string);
_initialized = true;
}
15 changes: 7 additions & 8 deletions src/hotspot/os/aix/attachListener_aix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ int AixAttachListener::init() {
::atexit(listener_cleanup);
}

int n = snprintf(path, UNIX_PATH_MAX, "%s/.java_pid%d",
os::get_temp_directory(), os::current_process_id());
int n = os::snprintf(path, UNIX_PATH_MAX, "%s/.java_pid%d",
os::get_temp_directory(), os::current_process_id());
if (n < (int)UNIX_PATH_MAX) {
n = snprintf(initial_path, UNIX_PATH_MAX, "%s.tmp", path);
n = os::snprintf(initial_path, UNIX_PATH_MAX, "%s.tmp", path);
}
if (n >= (int)UNIX_PATH_MAX) {
return -1;
Expand Down Expand Up @@ -349,9 +349,8 @@ void AttachListener::vm_start() {
struct stat st;
int ret;

int n = snprintf(fn, UNIX_PATH_MAX, "%s/.java_pid%d",
os::get_temp_directory(), os::current_process_id());
assert(n < (int)UNIX_PATH_MAX, "java_pid file name buffer overflow");
os::snprintf_checked(fn, UNIX_PATH_MAX, "%s/.java_pid%d",
os::get_temp_directory(), os::current_process_id());

RESTARTABLE(::stat(fn, &st), ret);
if (ret == 0) {
Expand Down Expand Up @@ -419,8 +418,8 @@ bool AttachListener::is_init_trigger() {
RESTARTABLE(::stat(fn, &st), ret);
if (ret == -1) {
log_trace(attach)("Failed to find attach file: %s, trying alternate", fn);
snprintf(fn, sizeof(fn), "%s/.attach_pid%d",
os::get_temp_directory(), os::current_process_id());
os::snprintf_checked(fn, sizeof(fn), "%s/.attach_pid%d",
os::get_temp_directory(), os::current_process_id());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could fail if os::get_temp_directory() returns an extremely long path. How about doing a truncation check like at line 354?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, same thing. We would need to fix a lot of code if os::get_temp_directory() returned a pathologically long string.

Copy link
Member Author

@dholmes-ora dholmes-ora Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed line 352 as per Kim's comment above because snprintf followed by an assert for truncation is what snprintf_checked does.

Again the question to ask is: if we hit this during testing do we think it indicates we need to increase the buffer size. Again I am initially in the yes camp.

RESTARTABLE(::stat(fn, &st), ret);
if (ret == -1) {
log_debug(attach)("Failed to find attach file: %s", fn);
Expand Down
8 changes: 4 additions & 4 deletions src/hotspot/os/aix/os_aix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1051,8 +1051,8 @@ static void* dll_load_library(const char *filename, int *eno, char *ebuf, int eb
error_report = "dlerror returned no error description";
}
if (ebuf != nullptr && ebuflen > 0) {
snprintf(ebuf, ebuflen - 1, "%s, LIBPATH=%s, LD_LIBRARY_PATH=%s : %s",
filename, ::getenv("LIBPATH"), ::getenv("LD_LIBRARY_PATH"), error_report);
os::snprintf_checked(ebuf, ebuflen - 1, "%s, LIBPATH=%s, LD_LIBRARY_PATH=%s : %s",
filename, ::getenv("LIBPATH"), ::getenv("LD_LIBRARY_PATH"), error_report);
}
Events::log_dll_message(nullptr, "Loading shared library %s failed, %s", filename, error_report);
log_info(os)("shared library load of %s failed, %s", filename, error_report);
Expand All @@ -1077,7 +1077,7 @@ void *os::dll_load(const char *filename, char *ebuf, int ebuflen) {
STATIC_ASSERT(sizeof(old_extension) >= sizeof(new_extension));
char* tmp_path = os::strdup(filename);
size_t prefix_size = pointer_delta(pointer_to_dot, filename, 1);
os::snprintf(tmp_path + prefix_size, sizeof(old_extension), "%s", new_extension);
os::snprintf_checked(tmp_path + prefix_size, sizeof(old_extension), "%s", new_extension);
result = dll_load_library(tmp_path, &eno, ebuf, ebuflen);
os::free(tmp_path);
}
Expand All @@ -1094,7 +1094,7 @@ void os::get_summary_os_info(char* buf, size_t buflen) {
// There might be something more readable than uname results for AIX.
struct utsname name;
uname(&name);
snprintf(buf, buflen, "%s %s", name.release, name.version);
os::snprintf_checked(buf, buflen, "%s %s", name.release, name.version);
}

int os::get_loaded_modules_info(os::LoadedModulesCallbackFunc callback, void *param) {
Expand Down
5 changes: 2 additions & 3 deletions src/hotspot/os/aix/porting_aix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ static const char* rtv_linkedin_libpath() {

// retrieve the path to the currently running executable binary
// to open it
snprintf(buffer, 100, "/proc/%ld/object/a.out", (long)getpid());
os::snprintf_checked(buffer, 100, "/proc/%ld/object/a.out", (long)getpid());
FILE* f = nullptr;
struct xcoffhdr the_xcoff;
struct scnhdr the_scn;
Expand Down Expand Up @@ -1154,7 +1154,7 @@ bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) {
error_report = "dlerror returned no error description";
}
if (ebuf != nullptr && ebuflen > 0) {
snprintf(ebuf, ebuflen - 1, "%s", error_report);
os::snprintf_checked(ebuf, ebuflen - 1, "%s", error_report);
}
assert(false, "os::pd_dll_unload() ::dlclose() failed");
}
Expand Down Expand Up @@ -1189,4 +1189,3 @@ bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) {

return res;
} // end: os::pd_dll_unload()

2 changes: 1 addition & 1 deletion src/hotspot/os/bsd/memMapPrinter_macosx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class MappingInfo {
X1(GENEALOGY, genealogy);
default:
static char buffer[30];
snprintf(buffer, sizeof(buffer), "user_tag=0x%x(%d)", user_tag, user_tag);
os::snprintf_checked(buffer, sizeof(buffer), "user_tag=0x%x(%d)", user_tag, user_tag);
return buffer;
}
}
Expand Down
Loading