From 2f33c1ff84c32ee207b6e75c34497c589823cb1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20H=C3=BCbner?= Date: Mon, 20 Oct 2025 17:44:25 +0200 Subject: [PATCH 1/2] Remove redundant null termination. --- src/hotspot/cpu/zero/frame_zero.cpp | 2 -- src/hotspot/os/linux/os_perf_linux.cpp | 6 ------ src/hotspot/share/cds/filemap.cpp | 1 - 3 files changed, 9 deletions(-) diff --git a/src/hotspot/cpu/zero/frame_zero.cpp b/src/hotspot/cpu/zero/frame_zero.cpp index 52ccad2fa68b5..69bbea2972a63 100644 --- a/src/hotspot/cpu/zero/frame_zero.cpp +++ b/src/hotspot/cpu/zero/frame_zero.cpp @@ -245,8 +245,6 @@ void frame::zero_print_on_error(int frame_index, 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'; // Print the result st->print_cr(" " PTR_FORMAT ": %-21s = %s", p2i(addr), fieldbuf, valuebuf); diff --git a/src/hotspot/os/linux/os_perf_linux.cpp b/src/hotspot/os/linux/os_perf_linux.cpp index 7caf8f98a00fb..5708194521f5f 100644 --- a/src/hotspot/os/linux/os_perf_linux.cpp +++ b/src/hotspot/os/linux/os_perf_linux.cpp @@ -705,11 +705,9 @@ bool SystemProcessInterface::SystemProcesses::ProcessIterator::is_valid_entry(st if (atoi(entry->d_name) != 0) { jio_snprintf(buffer, PATH_MAX, "/proc/%s", entry->d_name); - buffer[PATH_MAX - 1] = '\0'; if (is_dir(buffer)) { jio_snprintf(buffer, PATH_MAX, "/proc/%s/stat", entry->d_name); - buffer[PATH_MAX - 1] = '\0'; if (fsize(buffer, size) != OS_ERR) { return true; } @@ -724,7 +722,6 @@ void SystemProcessInterface::SystemProcesses::ProcessIterator::get_exe_name() { char buffer[PATH_MAX]; jio_snprintf(buffer, PATH_MAX, "/proc/%s/stat", _entry->d_name); - buffer[PATH_MAX - 1] = '\0'; if ((fp = os::fopen(buffer, "r")) != nullptr) { if (fgets(buffer, PATH_MAX, fp) != nullptr) { char* start, *end; @@ -752,7 +749,6 @@ char* SystemProcessInterface::SystemProcesses::ProcessIterator::get_cmdline() { char* cmdline = nullptr; jio_snprintf(buffer, PATH_MAX, "/proc/%s/cmdline", _entry->d_name); - buffer[PATH_MAX - 1] = '\0'; if ((fp = os::fopen(buffer, "r")) != nullptr) { size_t size = 0; char dummy; @@ -787,7 +783,6 @@ char* SystemProcessInterface::SystemProcesses::ProcessIterator::get_exe_path() { char buffer[PATH_MAX]; jio_snprintf(buffer, PATH_MAX, "/proc/%s/exe", _entry->d_name); - buffer[PATH_MAX - 1] = '\0'; return os::realpath(buffer, _exePath, PATH_MAX); } @@ -1001,7 +996,6 @@ int64_t NetworkPerformanceInterface::NetworkPerformance::read_counter(const char return -1; } - buf[num_bytes] = '\0'; int64_t value = strtoll(buf, nullptr, 10); return value; diff --git a/src/hotspot/share/cds/filemap.cpp b/src/hotspot/share/cds/filemap.cpp index 8c2175622e92f..050c1708efb1f 100644 --- a/src/hotspot/share/cds/filemap.cpp +++ b/src/hotspot/share/cds/filemap.cpp @@ -117,7 +117,6 @@ template static void get_header_version(char (&header_version) [N]) { // Append the hash code as eight hex digits. os::snprintf_checked(&header_version[JVM_IDENT_MAX-9], 9, "%08x", hash); - header_version[JVM_IDENT_MAX-1] = 0; // Null terminate. } assert(header_version[JVM_IDENT_MAX-1] == 0, "must be"); From 18bcd59fefd668a23a3fd2bbad635581ac2344b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20H=C3=BCbner?= Date: Tue, 21 Oct 2025 10:14:31 +0200 Subject: [PATCH 2/2] Remove unneccessary null terminator reservations. --- src/hotspot/os/aix/os_aix.cpp | 2 +- src/hotspot/os/aix/porting_aix.cpp | 3 ++- src/hotspot/os/bsd/os_bsd.cpp | 2 +- src/hotspot/os/linux/os_linux.cpp | 2 +- src/hotspot/os/windows/os_windows.cpp | 4 ++-- src/hotspot/share/runtime/reflection.cpp | 8 ++++---- src/hotspot/share/services/diagnosticFramework.cpp | 6 +++--- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/hotspot/os/aix/os_aix.cpp b/src/hotspot/os/aix/os_aix.cpp index 0c0c2808fa10f..5f81912c0d656 100644 --- a/src/hotspot/os/aix/os_aix.cpp +++ b/src/hotspot/os/aix/os_aix.cpp @@ -1054,7 +1054,7 @@ 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) { - os::snprintf_checked(ebuf, ebuflen - 1, "%s, LIBPATH=%s, LD_LIBRARY_PATH=%s : %s", + os::snprintf_checked(ebuf, ebuflen, "%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); diff --git a/src/hotspot/os/aix/porting_aix.cpp b/src/hotspot/os/aix/porting_aix.cpp index 402abd7d579de..7311afc197b8c 100644 --- a/src/hotspot/os/aix/porting_aix.cpp +++ b/src/hotspot/os/aix/porting_aix.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2012, 2024 SAP SE. All rights reserved. + * Copyright (c) 2025, 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 @@ -1154,7 +1155,7 @@ bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) { error_report = "dlerror returned no error description"; } if (ebuf != nullptr && ebuflen > 0) { - os::snprintf_checked(ebuf, ebuflen - 1, "%s", error_report); + os::snprintf_checked(ebuf, ebuflen, "%s", error_report); } assert(false, "os::pd_dll_unload() ::dlclose() failed"); } diff --git a/src/hotspot/os/bsd/os_bsd.cpp b/src/hotspot/os/bsd/os_bsd.cpp index 8c5bbd58a8437..dfa6353db85cd 100644 --- a/src/hotspot/os/bsd/os_bsd.cpp +++ b/src/hotspot/os/bsd/os_bsd.cpp @@ -2495,7 +2495,7 @@ bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) { error_report = "dlerror returned no error description"; } if (ebuf != nullptr && ebuflen > 0) { - os::snprintf_checked(ebuf, ebuflen - 1, "%s", error_report); + os::snprintf_checked(ebuf, ebuflen, "%s", error_report); } } diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index 772b170d11ca9..1a2e832ca11db 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -5443,7 +5443,7 @@ bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) { error_report = "dlerror returned no error description"; } if (ebuf != nullptr && ebuflen > 0) { - os::snprintf_checked(ebuf, ebuflen - 1, "%s", error_report); + os::snprintf_checked(ebuf, ebuflen, "%s", error_report); } } diff --git a/src/hotspot/os/windows/os_windows.cpp b/src/hotspot/os/windows/os_windows.cpp index ba05d390c9fe1..c841b324d62f4 100644 --- a/src/hotspot/os/windows/os_windows.cpp +++ b/src/hotspot/os/windows/os_windows.cpp @@ -1827,12 +1827,12 @@ void * os::dll_load(const char *name, char *ebuf, int ebuflen) { } if (lib_arch_str != nullptr) { - os::snprintf_checked(ebuf, ebuflen - 1, + os::snprintf_checked(ebuf, ebuflen, "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 - os::snprintf_checked(ebuf, ebuflen - 1, + os::snprintf_checked(ebuf, ebuflen, "Can't load this .dll (machine code=0x%x) on a %s-bit platform", lib_arch, running_arch_str); } diff --git a/src/hotspot/share/runtime/reflection.cpp b/src/hotspot/share/runtime/reflection.cpp index 7728643c64013..d6c10c5c70c08 100644 --- a/src/hotspot/share/runtime/reflection.cpp +++ b/src/hotspot/share/runtime/reflection.cpp @@ -546,7 +546,7 @@ char* Reflection::verify_class_access_msg(const Klass* current_class, size_t len = 100 + strlen(current_class_name) + 2*strlen(module_from_name) + strlen(new_class_name) + 2*strlen(module_to_name); msg = NEW_RESOURCE_ARRAY(char, len); - jio_snprintf(msg, len - 1, + jio_snprintf(msg, len, "class %s (in module %s) cannot access class %s (in module %s) because module %s does not read module %s", current_class_name, module_from_name, new_class_name, module_to_name, module_from_name, module_to_name); @@ -557,7 +557,7 @@ char* Reflection::verify_class_access_msg(const Klass* current_class, size_t len = 160 + strlen(current_class_name) + 2*strlen(module_from_name) + strlen(new_class_name) + 2*sizeof(uintx); msg = NEW_RESOURCE_ARRAY(char, len); - jio_snprintf(msg, len - 1, + jio_snprintf(msg, len, "class %s (in module %s) cannot access class %s (in unnamed module @0x%zx) because module %s does not read unnamed module @0x%zx", current_class_name, module_from_name, new_class_name, uintx(identity_hash), module_from_name, uintx(identity_hash)); @@ -573,7 +573,7 @@ char* Reflection::verify_class_access_msg(const Klass* current_class, size_t len = 118 + strlen(current_class_name) + 2*strlen(module_from_name) + strlen(new_class_name) + 2*strlen(module_to_name) + strlen(package_name); msg = NEW_RESOURCE_ARRAY(char, len); - jio_snprintf(msg, len - 1, + jio_snprintf(msg, len, "class %s (in module %s) cannot access class %s (in module %s) because module %s does not export %s to module %s", current_class_name, module_from_name, new_class_name, 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, size_t len = 170 + strlen(current_class_name) + strlen(new_class_name) + 2*strlen(module_to_name) + strlen(package_name) + 2*sizeof(uintx); msg = NEW_RESOURCE_ARRAY(char, len); - jio_snprintf(msg, len - 1, + jio_snprintf(msg, len, "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", current_class_name, uintx(identity_hash), new_class_name, module_to_name, module_to_name, package_name, uintx(identity_hash)); diff --git a/src/hotspot/share/services/diagnosticFramework.cpp b/src/hotspot/share/services/diagnosticFramework.cpp index 7cf41750f5d0a..c37fe7b4e1ea5 100644 --- a/src/hotspot/share/services/diagnosticFramework.cpp +++ b/src/hotspot/share/services/diagnosticFramework.cpp @@ -208,7 +208,7 @@ void DCmdParser::parse(CmdLine* line, char delim, TRAPS) { strncpy(argbuf, iter.key_addr(), len); argbuf[len] = '\0'; - jio_snprintf(buf, buflen - 1, "Unknown argument '%s' in diagnostic command.", argbuf); + jio_snprintf(buf, buflen, "Unknown argument '%s' in diagnostic command.", argbuf); THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf); } @@ -236,7 +236,7 @@ void DCmdParser::check(TRAPS) { GenDCmdArgument* arg = _arguments_list; while (arg != nullptr) { if (arg->is_mandatory() && !arg->has_value()) { - jio_snprintf(buf, buflen - 1, "The argument '%s' is mandatory.", arg->name()); + jio_snprintf(buf, buflen, "The argument '%s' is mandatory.", arg->name()); THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf); } arg = arg->next(); @@ -244,7 +244,7 @@ void DCmdParser::check(TRAPS) { arg = _options; while (arg != nullptr) { if (arg->is_mandatory() && !arg->has_value()) { - jio_snprintf(buf, buflen - 1, "The option '%s' is mandatory.", arg->name()); + jio_snprintf(buf, buflen, "The option '%s' is mandatory.", arg->name()); THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf); } arg = arg->next();