]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Avoid memory leaks when logging to MS Windows syslog (#2154)
authorJoshua Rogers <MegaManSec@users.noreply.github.com>
Mon, 8 Sep 2025 09:37:01 +0000 (09:37 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Mon, 8 Sep 2025 09:37:13 +0000 (09:37 +0000)
compat/mswindows.cc

index 5d24b04a96726a192cefe2518db25b6d56dd7be5..6ce80cd3797ffe87a23e605f465a8c915bc49bef 100644 (file)
@@ -24,6 +24,7 @@
 #include <cassert>
 #include <cstring>
 #include <fcntl.h>
+#include <memory>
 #include <sys/timeb.h>
 #if HAVE_PSAPI_H
 #include <psapi.h>
@@ -204,7 +205,6 @@ void
 syslog(int priority, const char *fmt, ...)
 {
     WORD logtype;
-    char *str=static_cast<char *>(xmalloc(SYSLOG_MAX_MSG_SIZE));
     int str_len;
     va_list ap;
 
@@ -212,7 +212,8 @@ syslog(int priority, const char *fmt, ...)
         return;
 
     va_start(ap, fmt);
-    str_len = vsnprintf(str, SYSLOG_MAX_MSG_SIZE-1, fmt, ap);
+    auto buf = std::make_unique<char[]>(SYSLOG_MAX_MSG_SIZE);
+    str_len = vsnprintf(buf.get(), SYSLOG_MAX_MSG_SIZE, fmt, ap);
     va_end(ap);
 
     if (str_len < 0) {
@@ -241,8 +242,9 @@ syslog(int priority, const char *fmt, ...)
     }
 
     //Windows API suck. They are overengineered
+    const auto strings[1] = { buf.get() };
     ReportEventA(ms_eventlog, logtype, 0, 0, nullptr, 1, 0,
-                 const_cast<const char **>(&str), nullptr);
+                 strings, nullptr);
 }
 
 /* note: this is all MSWindows-specific code; all of it should be conditional */