git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] win32: syslog: prevent potential realloc memory leak

From
Arjun Sreedharan <arjun024@gmail.com>
Date
Dec 13, 2014, 09:41 UTC
Message-ID
<1418463676-1753-1-git-send-email-arjun024@gmail.com>

use a temporary variable to free the memory in case realloc() fails.

Signed-off-by: Arjun Sreedharan <arjun024@gmail.com>
---
 compat/win32/syslog.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/compat/win32/syslog.c b/compat/win32/syslog.c
index d015e43..3409e43 100644
--- a/compat/win32/syslog.c
+++ b/compat/win32/syslog.c
@@ -16,7 +16,7 @@ void openlog(const char *ident, int logopt, int facility)
 void syslog(int priority, const char *fmt, ...)
 {
 	WORD logtype;
-	char *str, *pos;
+	char *str, *str_temp, *pos;
 	int str_len;
 	va_list ap;
 
@@ -43,9 +43,11 @@ void syslog(int priority, const char *fmt, ...)
 	va_end(ap);
 
 	while ((pos = strstr(str, "%1")) != NULL) {
+		str_temp = str;
 		str = realloc(str, ++str_len + 1);
 		if (!str) {
 			warning("realloc failed: '%s'", strerror(errno));
+			free(str_temp);
 			return;
 		}
 		memmove(pos + 2, pos + 1, strlen(pos));
-- 
1.8.1.msysgit.1
Next: Junio C Hamano
Message 1 of 4 in “win32: syslog: prevent potential realloc memory leak”
  1. win32: syslog: prevent potential realloc memory leakArjun Sreedharan, Dec 13, 2014
  2. Junio C HamanoDec 15, 2014
  3. Erik Faye-LundJan 25, 2015
  4. Junio C HamanoJan 26, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.