Skip to content

Commit 0f34b02

Browse files
Arrayingcoleenp
authored andcommitted
8365896: Remove unnecessary explicit buffer nul-termination after using os::snprintf
Reviewed-by: dholmes, coleenp
1 parent 6964ced commit 0f34b02

File tree

10 files changed

+14
-22
lines changed

10 files changed

+14
-22
lines changed

src/hotspot/cpu/zero/frame_zero.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ void frame::zero_print_on_error(int frame_index,
245245
os::snprintf_checked(fieldbuf, buflen, "word[%d]", offset);
246246
os::snprintf_checked(valuebuf, buflen, PTR_FORMAT, *addr);
247247
zeroframe()->identify_word(frame_index, offset, fieldbuf, valuebuf, buflen);
248-
fieldbuf[buflen - 1] = '\0';
249-
valuebuf[buflen - 1] = '\0';
250248

251249
// Print the result
252250
st->print_cr(" " PTR_FORMAT ": %-21s = %s", p2i(addr), fieldbuf, valuebuf);

src/hotspot/os/aix/os_aix.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ static void* dll_load_library(const char *filename, int *eno, char *ebuf, int eb
10541054
error_report = "dlerror returned no error description";
10551055
}
10561056
if (ebuf != nullptr && ebuflen > 0) {
1057-
os::snprintf_checked(ebuf, ebuflen - 1, "%s, LIBPATH=%s, LD_LIBRARY_PATH=%s : %s",
1057+
os::snprintf_checked(ebuf, ebuflen, "%s, LIBPATH=%s, LD_LIBRARY_PATH=%s : %s",
10581058
filename, ::getenv("LIBPATH"), ::getenv("LD_LIBRARY_PATH"), error_report);
10591059
}
10601060
Events::log_dll_message(nullptr, "Loading shared library %s failed, %s", filename, error_report);

src/hotspot/os/aix/porting_aix.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2012, 2024 SAP SE. All rights reserved.
3+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
34
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45
*
56
* This code is free software; you can redistribute it and/or modify it
@@ -1154,7 +1155,7 @@ bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) {
11541155
error_report = "dlerror returned no error description";
11551156
}
11561157
if (ebuf != nullptr && ebuflen > 0) {
1157-
os::snprintf_checked(ebuf, ebuflen - 1, "%s", error_report);
1158+
os::snprintf_checked(ebuf, ebuflen, "%s", error_report);
11581159
}
11591160
assert(false, "os::pd_dll_unload() ::dlclose() failed");
11601161
}

src/hotspot/os/bsd/os_bsd.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2483,7 +2483,7 @@ bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) {
24832483
error_report = "dlerror returned no error description";
24842484
}
24852485
if (ebuf != nullptr && ebuflen > 0) {
2486-
os::snprintf_checked(ebuf, ebuflen - 1, "%s", error_report);
2486+
os::snprintf_checked(ebuf, ebuflen, "%s", error_report);
24872487
}
24882488
}
24892489

src/hotspot/os/linux/os_linux.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5338,7 +5338,7 @@ bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) {
53385338
error_report = "dlerror returned no error description";
53395339
}
53405340
if (ebuf != nullptr && ebuflen > 0) {
5341-
os::snprintf_checked(ebuf, ebuflen - 1, "%s", error_report);
5341+
os::snprintf_checked(ebuf, ebuflen, "%s", error_report);
53425342
}
53435343
}
53445344

src/hotspot/os/linux/os_perf_linux.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -705,11 +705,9 @@ bool SystemProcessInterface::SystemProcesses::ProcessIterator::is_valid_entry(st
705705

706706
if (atoi(entry->d_name) != 0) {
707707
jio_snprintf(buffer, PATH_MAX, "/proc/%s", entry->d_name);
708-
buffer[PATH_MAX - 1] = '\0';
709708

710709
if (is_dir(buffer)) {
711710
jio_snprintf(buffer, PATH_MAX, "/proc/%s/stat", entry->d_name);
712-
buffer[PATH_MAX - 1] = '\0';
713711
if (fsize(buffer, size) != OS_ERR) {
714712
return true;
715713
}
@@ -724,7 +722,6 @@ void SystemProcessInterface::SystemProcesses::ProcessIterator::get_exe_name() {
724722
char buffer[PATH_MAX];
725723

726724
jio_snprintf(buffer, PATH_MAX, "/proc/%s/stat", _entry->d_name);
727-
buffer[PATH_MAX - 1] = '\0';
728725
if ((fp = os::fopen(buffer, "r")) != nullptr) {
729726
if (fgets(buffer, PATH_MAX, fp) != nullptr) {
730727
char* start, *end;
@@ -752,7 +749,6 @@ char* SystemProcessInterface::SystemProcesses::ProcessIterator::get_cmdline() {
752749
char* cmdline = nullptr;
753750

754751
jio_snprintf(buffer, PATH_MAX, "/proc/%s/cmdline", _entry->d_name);
755-
buffer[PATH_MAX - 1] = '\0';
756752
if ((fp = os::fopen(buffer, "r")) != nullptr) {
757753
size_t size = 0;
758754
char dummy;
@@ -787,7 +783,6 @@ char* SystemProcessInterface::SystemProcesses::ProcessIterator::get_exe_path() {
787783
char buffer[PATH_MAX];
788784

789785
jio_snprintf(buffer, PATH_MAX, "/proc/%s/exe", _entry->d_name);
790-
buffer[PATH_MAX - 1] = '\0';
791786
return os::realpath(buffer, _exePath, PATH_MAX);
792787
}
793788

@@ -1001,7 +996,6 @@ int64_t NetworkPerformanceInterface::NetworkPerformance::read_counter(const char
1001996
return -1;
1002997
}
1003998

1004-
buf[num_bytes] = '\0';
1005999
int64_t value = strtoll(buf, nullptr, 10);
10061000

10071001
return value;

src/hotspot/os/windows/os_windows.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,12 +1827,12 @@ void * os::dll_load(const char *name, char *ebuf, int ebuflen) {
18271827
}
18281828

18291829
if (lib_arch_str != nullptr) {
1830-
os::snprintf_checked(ebuf, ebuflen - 1,
1830+
os::snprintf_checked(ebuf, ebuflen,
18311831
"Can't load %s-bit .dll on a %s-bit platform",
18321832
lib_arch_str, running_arch_str);
18331833
} else {
18341834
// don't know what architecture this dll was build for
1835-
os::snprintf_checked(ebuf, ebuflen - 1,
1835+
os::snprintf_checked(ebuf, ebuflen,
18361836
"Can't load this .dll (machine code=0x%x) on a %s-bit platform",
18371837
lib_arch, running_arch_str);
18381838
}

src/hotspot/share/cds/filemap.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ template <int N> static void get_header_version(char (&header_version) [N]) {
117117

118118
// Append the hash code as eight hex digits.
119119
os::snprintf_checked(&header_version[JVM_IDENT_MAX-9], 9, "%08x", hash);
120-
header_version[JVM_IDENT_MAX-1] = 0; // Null terminate.
121120
}
122121

123122
assert(header_version[JVM_IDENT_MAX-1] == 0, "must be");

src/hotspot/share/runtime/reflection.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ char* Reflection::verify_class_access_msg(const Klass* current_class,
546546
size_t len = 100 + strlen(current_class_name) + 2*strlen(module_from_name) +
547547
strlen(new_class_name) + 2*strlen(module_to_name);
548548
msg = NEW_RESOURCE_ARRAY(char, len);
549-
jio_snprintf(msg, len - 1,
549+
jio_snprintf(msg, len,
550550
"class %s (in module %s) cannot access class %s (in module %s) because module %s does not read module %s",
551551
current_class_name, module_from_name, new_class_name,
552552
module_to_name, module_from_name, module_to_name);
@@ -557,7 +557,7 @@ char* Reflection::verify_class_access_msg(const Klass* current_class,
557557
size_t len = 160 + strlen(current_class_name) + 2*strlen(module_from_name) +
558558
strlen(new_class_name) + 2*sizeof(uintx);
559559
msg = NEW_RESOURCE_ARRAY(char, len);
560-
jio_snprintf(msg, len - 1,
560+
jio_snprintf(msg, len,
561561
"class %s (in module %s) cannot access class %s (in unnamed module @0x%zx) because module %s does not read unnamed module @0x%zx",
562562
current_class_name, module_from_name, new_class_name, uintx(identity_hash),
563563
module_from_name, uintx(identity_hash));
@@ -573,7 +573,7 @@ char* Reflection::verify_class_access_msg(const Klass* current_class,
573573
size_t len = 118 + strlen(current_class_name) + 2*strlen(module_from_name) +
574574
strlen(new_class_name) + 2*strlen(module_to_name) + strlen(package_name);
575575
msg = NEW_RESOURCE_ARRAY(char, len);
576-
jio_snprintf(msg, len - 1,
576+
jio_snprintf(msg, len,
577577
"class %s (in module %s) cannot access class %s (in module %s) because module %s does not export %s to module %s",
578578
current_class_name, module_from_name, new_class_name,
579579
module_to_name, module_to_name, package_name, module_from_name);
@@ -584,7 +584,7 @@ char* Reflection::verify_class_access_msg(const Klass* current_class,
584584
size_t len = 170 + strlen(current_class_name) + strlen(new_class_name) +
585585
2*strlen(module_to_name) + strlen(package_name) + 2*sizeof(uintx);
586586
msg = NEW_RESOURCE_ARRAY(char, len);
587-
jio_snprintf(msg, len - 1,
587+
jio_snprintf(msg, len,
588588
"class %s (in unnamed module @0x%zx) cannot access class %s (in module %s) because module %s does not export %s to unnamed module @0x%zx",
589589
current_class_name, uintx(identity_hash), new_class_name, module_to_name,
590590
module_to_name, package_name, uintx(identity_hash));

src/hotspot/share/services/diagnosticFramework.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ void DCmdParser::parse(CmdLine* line, char delim, TRAPS) {
208208

209209
strncpy(argbuf, iter.key_addr(), len);
210210
argbuf[len] = '\0';
211-
jio_snprintf(buf, buflen - 1, "Unknown argument '%s' in diagnostic command.", argbuf);
211+
jio_snprintf(buf, buflen, "Unknown argument '%s' in diagnostic command.", argbuf);
212212

213213
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf);
214214
}
@@ -236,15 +236,15 @@ void DCmdParser::check(TRAPS) {
236236
GenDCmdArgument* arg = _arguments_list;
237237
while (arg != nullptr) {
238238
if (arg->is_mandatory() && !arg->has_value()) {
239-
jio_snprintf(buf, buflen - 1, "The argument '%s' is mandatory.", arg->name());
239+
jio_snprintf(buf, buflen, "The argument '%s' is mandatory.", arg->name());
240240
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf);
241241
}
242242
arg = arg->next();
243243
}
244244
arg = _options;
245245
while (arg != nullptr) {
246246
if (arg->is_mandatory() && !arg->has_value()) {
247-
jio_snprintf(buf, buflen - 1, "The option '%s' is mandatory.", arg->name());
247+
jio_snprintf(buf, buflen, "The option '%s' is mandatory.", arg->name());
248248
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf);
249249
}
250250
arg = arg->next();

0 commit comments

Comments
 (0)