Module Name:    src
Committed By:   christos
Date:           Thu Mar  7 22:08:59 UTC 2019

Modified Files:
        src/usr.sbin/makemandb: apropos-utils.c

Log Message:
fix memory allocation problems detected by jemalloc...


To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 src/usr.sbin/makemandb/apropos-utils.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/makemandb/apropos-utils.c
diff -u src/usr.sbin/makemandb/apropos-utils.c:1.40 src/usr.sbin/makemandb/apropos-utils.c:1.41
--- src/usr.sbin/makemandb/apropos-utils.c:1.40	Sat Nov 25 09:29:38 2017
+++ src/usr.sbin/makemandb/apropos-utils.c	Thu Mar  7 17:08:59 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: apropos-utils.c,v 1.40 2017/11/25 14:29:38 abhinav Exp $	*/
+/*	$NetBSD: apropos-utils.c,v 1.41 2019/03/07 22:08:59 christos Exp $	*/
 /*-
  * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadh...@gmail.com>
  * All rights reserved.
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: apropos-utils.c,v 1.40 2017/11/25 14:29:38 abhinav Exp $");
+__RCSID("$NetBSD: apropos-utils.c,v 1.41 2019/03/07 22:08:59 christos Exp $");
 
 #include <sys/queue.h>
 #include <sys/stat.h>
@@ -270,34 +270,40 @@ unzip(sqlite3_context *pctx, int nval, s
 	unsigned int rc;
 	unsigned char *outbuf;
 	z_stream stream;
+	long total_out;
 
 	assert(nval == 1);
+	memset(&stream, 0, sizeof(stream));
 	stream.next_in = __UNCONST(sqlite3_value_blob(apval[0]));
 	stream.avail_in = sqlite3_value_bytes(apval[0]);
-	stream.avail_out = stream.avail_in * 2 + 100;
-	stream.next_out = outbuf = emalloc(stream.avail_out);
 	stream.zalloc = NULL;
 	stream.zfree = NULL;
 
 	if (inflateInit(&stream) != Z_OK) {
-		free(outbuf);
 		return;
 	}
 
+	total_out = stream.avail_out = stream.avail_in * 2 + 100;
+	stream.next_out = outbuf = emalloc(stream.avail_out);
 	while ((rc = inflate(&stream, Z_SYNC_FLUSH)) != Z_STREAM_END) {
 		if (rc != Z_OK ||
 		    (stream.avail_out != 0 && stream.avail_in == 0)) {
 			free(outbuf);
 			return;
 		}
-		outbuf = erealloc(outbuf, stream.total_out * 2);
+		total_out <<= 1;
+		outbuf = erealloc(outbuf, total_out);
 		stream.next_out = outbuf + stream.total_out;
-		stream.avail_out = stream.total_out;
+		stream.avail_out = total_out - stream.total_out;
 	}
 	if (inflateEnd(&stream) != Z_OK) {
 		free(outbuf);
 		return;
 	}
+	if (stream.total_out == 0) {
+		free(outbuf);
+		return;
+	}
 	outbuf = erealloc(outbuf, stream.total_out);
 	sqlite3_result_text(pctx, (const char *)outbuf, stream.total_out, free);
 }

Reply via email to