fix: Handle Input/Output Errors in vsnprintf and snprintf (#35898)

Fix for Issue: #35897
This commit is contained in:
Abdullah Ahmed 2024-09-04 05:45:04 +05:30 committed by GitHub
parent 69b1eea6f2
commit cc02dc0a55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -46,9 +46,20 @@ LogOut(const char* pattern, ...) {
va_list vl;
va_start(vl, pattern);
vsnprintf(str_p.get(), len, pattern, vl); // NOLINT
int result = vsnprintf(str_p.get(), len, pattern, vl); // NOLINT
va_end(vl);
if (result < 0) {
std::cerr << "Error: vsnprintf failed to format the string."
<< std::endl;
return "Formatting Error";
} else if (static_cast<size_t>(result) >= len) {
std::cerr
<< "Warning: Output was truncated. Buffer size was insufficient."
<< std::endl;
return "Truncated Output";
}
return {str_p.get()};
}
@ -98,11 +109,20 @@ get_thread_starttime() {
int64_t pid = getpid();
char filename[256];
snprintf(filename,
sizeof(filename),
"/proc/%lld/task/%lld/stat",
(long long)pid, // NOLINT, TODO: How to solve this?
(long long)tid); // NOLINT
int ret_snprintf =
snprintf(filename,
sizeof(filename),
"/proc/%lld/task/%lld/stat",
(long long)pid, // NOLINT, TODO: How to solve this?
(long long)tid); // NOLINT
if (ret_snprintf < 0 ||
static_cast<size_t>(ret_snprintf) >= sizeof(filename)) {
std::cerr << "Error: snprintf failed or output was truncated when "
"creating filename."
<< std::endl;
throw std::runtime_error("Failed to format filename string.");
}
int64_t val = 0;
char comm[16], state;