summaryrefslogtreecommitdiff
path: root/usr.sbin/makemandb
diff options
context:
space:
mode:
authorchristos <christos@NetBSD.org>2019-03-07 22:08:59 +0000
committerchristos <christos@NetBSD.org>2019-03-07 22:08:59 +0000
commitf2bde82841afb7e5e93f7fc1c5cebd85ffa13b6d (patch)
treeaafa21f96312eaf406ee94c206c14336f56b7819 /usr.sbin/makemandb
parent45c8e2c6dda696a97e4dc3bb940ff676554f1605 (diff)
fix memory allocation problems detected by jemalloc...
Diffstat (limited to 'usr.sbin/makemandb')
-rw-r--r--usr.sbin/makemandb/apropos-utils.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/usr.sbin/makemandb/apropos-utils.c b/usr.sbin/makemandb/apropos-utils.c
index 4ee8a2085fd..cfb5f4c7617 100644
--- a/usr.sbin/makemandb/apropos-utils.c
+++ b/usr.sbin/makemandb/apropos-utils.c
@@ -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.upadhyay@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, sqlite3_value **apval)
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);
}