From d8d8a29fc2a6e134c15dac64a919991b535acca4 Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Tue, 24 May 2022 17:20:40 +0200 Subject: [PATCH] fix memory issues with logs --- src/debug/Log.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/debug/Log.cpp b/src/debug/Log.cpp index 0acecb04..3a008758 100644 --- a/src/debug/Log.cpp +++ b/src/debug/Log.cpp @@ -5,8 +5,6 @@ #include void Debug::log(LogLevel level, const char* fmt, ...) { - va_list args; - va_start(args, fmt); // log to a file const std::string DEBUGPATH = ISDEBUG ? "/tmp/hypr/hyprlandd.log" : "/tmp/hypr/hyprland.log"; @@ -31,15 +29,36 @@ void Debug::log(LogLevel level, const char* fmt, ...) { } char buf[LOGMESSAGESIZE] = ""; + char* outputStr; + int logLen; - vsprintf(buf, fmt, args); + va_list args; + va_start(args, fmt); + logLen = vsnprintf(buf, sizeof buf, fmt, args); + va_end(args); - ofs << buf << "\n"; + if ((long unsigned int)logLen < sizeof buf) { + outputStr = strdup(buf); + } else { + outputStr = (char*)malloc(logLen + 1); + + if (!outputStr) { + printf("CRITICAL: Cannot alloc size %d for log! (Out of memory?)", logLen + 1); + return; + } + + va_start(args, fmt); + vsnprintf(outputStr, logLen + 1U, fmt, args); + va_end(args); + } + + ofs << outputStr << "\n"; ofs.close(); // log it to the stdout too. - std::cout << buf << "\n"; + std::cout << outputStr << "\n"; - va_end(args); + // free the log + free(outputStr); }