# [PATCH] win32: plug memory leak on realloc() failure in syslog()

2 messages from 2017-08-10 to 2017-08-14. Participants: René Scharfe, Johannes Schindelin.
Thread: https://gitlist.dev/t/46555

## René Scharfe, 2017-08-10 10:23

Subject: [PATCH] win32: plug memory leak on realloc() failure in syslog()
Message-ID: <245410ce-1f4c-f2a9-fd8d-98ff2d2c0335@web.de>
URL: https://gitlist.dev/e/245410ce-1f4c-f2a9-fd8d-98ff2d2c0335%40web.de

```
If realloc() fails then the original buffer is still valid.  Free it
before exiting the function.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 compat/win32/syslog.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/compat/win32/syslog.c b/compat/win32/syslog.c
index 6c7c9b6053..161978d720 100644
--- a/compat/win32/syslog.c
+++ b/compat/win32/syslog.c
@@ -43,8 +43,10 @@ void syslog(int priority, const char *fmt, ...)
 	va_end(ap);
 
 	while ((pos = strstr(str, "%1")) != NULL) {
+		char *oldstr = str;
 		str = realloc(str, st_add(++str_len, 1));
 		if (!str) {
+			free(oldstr);
 			warning_errno("realloc failed");
 			return;
 		}
-- 
2.14.0

```

## Johannes Schindelin, 2017-08-14 20:26

Subject: Re: [PATCH] win32: plug memory leak on realloc() failure in syslog()
Message-ID: <alpine.DEB.2.21.1.1708142225330.19382@virtualbox>
URL: https://gitlist.dev/e/alpine.DEB.2.21.1.1708142225330.19382%40virtualbox
In-Reply-To: <245410ce-1f4c-f2a9-fd8d-98ff2d2c0335@web.de>

```
Hi René,

On Thu, 10 Aug 2017, René Scharfe wrote:

> If realloc() fails then the original buffer is still valid.  Free it
> before exiting the function.
> 
> Signed-off-by: Rene Scharfe <l.s.r@web.de>

The subject had me worried for a second... The realloc() fails so rarely
that I, for one, have never encountered that problem.

Still, it is a correct fix for a real bug.

Thanks,
Dscho
```
