8285506: Unify os::vsnprintf implementations

Reviewed-by: jwaters, kbarrett, jsjolen
This commit is contained in:
David Holmes 2024-05-28 22:43:35 +00:00
parent 91ab088d5e
commit 673f767dad
4 changed files with 12 additions and 24 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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.

View File

@ -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);