Skip to content

Commit b1fb318

Browse files
committed
Make os::snprintf "nodiscard" and adjust final callsites
1 parent 3f99905 commit b1fb318

File tree

6 files changed

+7
-7
lines changed

6 files changed

+7
-7
lines changed

src/hotspot/os/bsd/os_bsd.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2156,7 +2156,7 @@ void os::set_native_thread_name(const char *name) {
21562156
if (name != nullptr) {
21572157
// Add a "Java: " prefix to the name
21582158
char buf[MAXTHREADNAMESIZE];
2159-
snprintf(buf, sizeof(buf), "Java: %s", name);
2159+
(void) os::snprintf(buf, sizeof(buf), "Java: %s", name);
21602160
pthread_setname_np(buf);
21612161
}
21622162
#endif

src/hotspot/os/linux/os_linux.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4797,7 +4797,7 @@ uint os::processor_id() {
47974797
void os::set_native_thread_name(const char *name) {
47984798
if (Linux::_pthread_setname_np) {
47994799
char buf [16]; // according to glibc manpage, 16 chars incl. '/0'
4800-
snprintf(buf, sizeof(buf), "%s", name);
4800+
(void) os::snprintf(buf, sizeof(buf), "%s", name);
48014801
buf[sizeof(buf) - 1] = '\0';
48024802
const int rc = Linux::_pthread_setname_np(pthread_self(), buf);
48034803
// ERANGE should not happen; all other errors should just be ignored.
@@ -5395,7 +5395,7 @@ bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) {
53955395
error_report = "dlerror returned no error description";
53965396
}
53975397
if (ebuf != nullptr && ebuflen > 0) {
5398-
snprintf(ebuf, ebuflen - 1, "%s", error_report);
5398+
os::snprintf_checked(ebuf, ebuflen - 1, "%s", error_report);
53995399
}
54005400
}
54015401

src/hotspot/share/oops/generateOopMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,7 @@ void GenerateOopMap::error_work(const char *format, va_list ap) {
21532153
(void) os::vsnprintf(msg_buffer, sizeof(msg_buffer), format, ap);
21542154
// Append method name
21552155
char msg_buffer2[512];
2156-
os::snprintf(msg_buffer2, sizeof(msg_buffer2), "%s in method %s", msg_buffer, method()->name()->as_C_string());
2156+
(void) os::snprintf(msg_buffer2, sizeof(msg_buffer2), "%s in method %s", msg_buffer, method()->name()->as_C_string());
21572157
Thread* current = Thread::current();
21582158
if (current->can_call_java()) {
21592159
_exception = Exceptions::new_exception(JavaThread::cast(current),

src/hotspot/share/runtime/os.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ class os: AllStatic {
800800
// an encoding error or any other kind of usage error).
801801
[[nodiscard]] static int vsnprintf(char* buf, size_t len, const char* fmt, va_list args) ATTRIBUTE_PRINTF(3, 0);
802802
// Delegates to vsnprintf.
803-
static int snprintf(char* buf, size_t len, const char* fmt, ...) ATTRIBUTE_PRINTF(3, 4);
803+
[[nodiscard]] static int snprintf(char* buf, size_t len, const char* fmt, ...) ATTRIBUTE_PRINTF(3, 4);
804804

805805
// Delegates to snprintf and asserts that the output was not truncated.
806806
static void snprintf_checked(char* buf, size_t len, const char* fmt, ...) ATTRIBUTE_PRINTF(3, 4);

test/hotspot/gtest/gtestMain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ static void runUnitTestsInner(int argc, char** argv) {
256256
#ifdef __APPLE__
257257
size_t len = strlen(java_home) + strlen("/lib/jli/libjli.dylib") + 1;
258258
char* path = new char[len];
259-
snprintf(path, len, "%s/lib/jli/libjli.dylib", java_home);
259+
os::snprintf_checked(path, len, "%s/lib/jli/libjli.dylib", java_home);
260260
dlopen(path, RTLD_NOW | RTLD_GLOBAL);
261261
#endif // __APPLE__
262262

test/hotspot/gtest/runtime/test_os_windows.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ static void record_path(char const* name, char const* len_name, wchar_t* path) {
376376

377377
if (convert_to_cstring(buf, JVM_MAXPATHLEN, path)) {
378378
::testing::Test::RecordProperty(name, buf);
379-
os::snprintf(buf, JVM_MAXPATHLEN, "%d", (int) wcslen(path));
379+
os::snprintf_checked(buf, JVM_MAXPATHLEN, "%d", (int) wcslen(path));
380380
::testing::Test::RecordProperty(len_name, buf);
381381
}
382382
}

0 commit comments

Comments
 (0)