shithub: sce

Download patch

ref: ba13bc224be1dfd31711e500f66b51bca21188f4
parent: abe39da4407afac58b57173b8322936051c7bfa5
author: qwx <qwx@sciops.net>
date: Fri Aug 14 19:53:32 EDT 2020

util: fix erealloc always clearing extra space even if shrinking buffer

erealloc is used here for arrays of structs and pointers
with the assumption that extra elements are zeroed out
when growing the buffer.
overwriting memory unconditionally is obviously stupid.

--- a/util.c
+++ b/util.c
@@ -47,7 +47,8 @@
 	if((p = realloc(p, n)) == nil)
 		sysfatal("realloc: %r");
 	setrealloctag(p, getcallerpc(&p));
-	memset((uchar *)p + oldn, 0, n - oldn);
+	if(n > oldn)
+		memset((uchar *)p + oldn, 0, n - oldn);
 	return p;
 }