8296812: sprintf is deprecated in Xcode 14

Reviewed-by: stuefe, prr, kbarrett, lucy
This commit is contained in:
Xue-Lei Andrew Fan 2022-12-09 22:37:20 +00:00
parent 715bf70459
commit 478ef389dc
26 changed files with 141 additions and 100 deletions

View file

@ -99,6 +99,16 @@ int os::snprintf(char* buf, size_t len, const char* fmt, ...) {
return result;
}
int os::snprintf_checked(char* buf, size_t len, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
int result = os::vsnprintf(buf, len, fmt, args);
va_end(args);
assert(result >= 0, "os::snprintf error");
assert(static_cast<size_t>(result) < len, "os::snprintf truncated");
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.
@ -1314,7 +1324,7 @@ char* os::format_boot_path(const char* format_string,
FILE* os::fopen(const char* path, const char* mode) {
char modified_mode[20];
assert(strlen(mode) + 1 < sizeof(modified_mode), "mode chars plus one extra must fit in buffer");
sprintf(modified_mode, "%s" LINUX_ONLY("e") BSD_ONLY("e") WINDOWS_ONLY("N"), mode);
os::snprintf_checked(modified_mode, sizeof(modified_mode), "%s" LINUX_ONLY("e") BSD_ONLY("e") WINDOWS_ONLY("N"), mode);
FILE* file = ::fopen(path, modified_mode);
#if !(defined LINUX || defined BSD || defined _WINDOWS)