git: 9front

Download patch

ref: 946a00708d95199113e9c9aa0849d2520bc413f4
parent: 6459fcff761a53c534f48106deee25a230022ad3
author: cinap_lenrek <cinap_lenrek@gmx.de>
date: Sun May 13 21:35:17 EDT 2012

libplumb: realloc memleak (thank you clsmith, THANK YOU!)

--- a/sys/src/libplumb/mesg.c
+++ b/sys/src/libplumb/mesg.c
@@ -411,7 +411,7 @@
 Plumbmsg*
 plumbrecv(int fd)
 {
-	char *buf;
+	char *buf, *old;
 	Plumbmsg *m;
 	int n, more;
 
@@ -424,9 +424,11 @@
 		m = plumbunpackpartial(buf, n, &more);
 		if(m==nil && more>0){
 			/* we now know how many more bytes to read for complete message */
-			buf = realloc(buf, n+more);
-			if(buf == nil)
+			buf = realloc(old = buf, n+more);
+			if(buf == nil){
+				free(old);
 				return nil;
+			}
 			if(readn(fd, buf+n, more) == more)
 				m = plumbunpackpartial(buf, n+more, nil);
 		}
--