From 673f767dadc8f3a784b9c31c406422846df3279b Mon Sep 17 00:00:00 2001 From: David Holmes Date: Tue, 28 May 2024 22:43:35 +0000 Subject: [PATCH] 8285506: Unify os::vsnprintf implementations Reviewed-by: jwaters, kbarrett, jsjolen --- src/hotspot/os/posix/os_posix.cpp | 11 ----------- src/hotspot/os/windows/os_windows.cpp | 11 ----------- src/hotspot/share/runtime/os.cpp | 10 ++++++++++ src/hotspot/share/runtime/os.hpp | 4 ++-- 4 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/hotspot/os/posix/os_posix.cpp b/src/hotspot/os/posix/os_posix.cpp index a18a633002c..1e7473eea1d 100644 --- a/src/hotspot/os/posix/os_posix.cpp +++ b/src/hotspot/os/posix/os_posix.cpp @@ -425,17 +425,6 @@ char* os::map_memory_to_file_aligned(size_t size, size_t alignment, int file_des return aligned_base; } -int os::vsnprintf(char* buf, size_t len, const char* fmt, va_list args) { - // All supported POSIX platforms provide C99 semantics. - ALLOW_C_FUNCTION(::vsnprintf, int result = ::vsnprintf(buf, len, fmt, args);) - // If an encoding error occurred (result < 0) then it's not clear - // whether the buffer is NUL terminated, so ensure it is. - if ((result < 0) && (len > 0)) { - buf[len - 1] = '\0'; - } - return result; -} - int os::get_fileno(FILE* fp) { return NOT_AIX(::)fileno(fp); } diff --git a/src/hotspot/os/windows/os_windows.cpp b/src/hotspot/os/windows/os_windows.cpp index 20422e83cb7..b95209b3f9b 100644 --- a/src/hotspot/os/windows/os_windows.cpp +++ b/src/hotspot/os/windows/os_windows.cpp @@ -1739,17 +1739,6 @@ void os::get_summary_os_info(char* buf, size_t buflen) { if (nl != nullptr) *nl = '\0'; } -int os::vsnprintf(char* buf, size_t len, const char* fmt, va_list args) { - // Starting with Visual Studio 2015, vsnprint is C99 compliant. - ALLOW_C_FUNCTION(::vsnprintf, int result = ::vsnprintf(buf, len, fmt, args);) - // If an encoding error occurred (result < 0) then it's not clear - // whether the buffer is NUL terminated, so ensure it is. - if ((result < 0) && (len > 0)) { - buf[len - 1] = '\0'; - } - return result; -} - static inline time_t get_mtime(const char* filename) { struct stat st; int ret = os::stat(filename, &st); diff --git a/src/hotspot/share/runtime/os.cpp b/src/hotspot/share/runtime/os.cpp index 53c438cd208..e7fed633c45 100644 --- a/src/hotspot/share/runtime/os.cpp +++ b/src/hotspot/share/runtime/os.cpp @@ -111,6 +111,16 @@ int os::snprintf_checked(char* buf, size_t len, const char* fmt, ...) { return result; } +int os::vsnprintf(char* buf, size_t len, const char* fmt, va_list args) { + ALLOW_C_FUNCTION(::vsnprintf, int result = ::vsnprintf(buf, len, fmt, args);) + // If an encoding error occurred (result < 0) then it's not clear + // whether the buffer is NUL terminated, so ensure it is. + if ((result < 0) && (len > 0)) { + buf[len - 1] = '\0'; + } + return result; +} + // Fill in buffer with current local time as an ISO-8601 string. // E.g., YYYY-MM-DDThh:mm:ss.mmm+zzzz. // Returns buffer, or null if it failed. diff --git a/src/hotspot/share/runtime/os.hpp b/src/hotspot/share/runtime/os.hpp index 971c3c884c4..a6626c1389f 100644 --- a/src/hotspot/share/runtime/os.hpp +++ b/src/hotspot/share/runtime/os.hpp @@ -771,8 +771,8 @@ class os: AllStatic { static void *find_agent_function(JvmtiAgent *agent_lib, bool check_lib, const char *syms[], size_t syms_len); - // Provide C99 compliant versions of these functions, since some versions - // of some platforms don't. + // Provide wrapper versions of these functions to guarantee NUL-termination + // in all cases. static int vsnprintf(char* buf, size_t len, const char* fmt, va_list args) ATTRIBUTE_PRINTF(3, 0); static int snprintf(char* buf, size_t len, const char* fmt, ...) ATTRIBUTE_PRINTF(3, 4);