Commit Diff


commit - 666b4ca8b6176a8c10c13952d1176ad8974c6c57
commit + 17d745b8721ea2b811c9876638c588496f56d269
blob - cacf5c9f7f1b49a0c9305af7eba8aecf8b576294
blob + 8d7a9afa256fea6f0cca689baee19fce6680df6e
--- lib/inflate.c
+++ lib/inflate.c
@@ -208,6 +208,7 @@ got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, F
 	size_t avail;
 	struct got_zstream_buf zb;
 	void *newbuf;
+	int nbuf = 1;
 
 	*outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
 	if (*outbuf == NULL)
@@ -224,8 +225,9 @@ got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, F
 			goto done;
 		*outlen += avail;
 		if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
-			newbuf = reallocarray(*outbuf, 1,
-			    *outlen + GOT_ZSTREAM_BUFSIZE);
+			nbuf++;
+			newbuf = recallocarray(*outbuf, nbuf - 1, nbuf,
+			   GOT_ZSTREAM_BUFSIZE);
 			if (newbuf == NULL) {
 				err = got_error_from_errno();
 				free(*outbuf);
@@ -235,7 +237,7 @@ got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, F
 			}
 			*outbuf = newbuf;
 			zb.outbuf = newbuf + *outlen;
-			zb.outlen = GOT_ZSTREAM_BUFSIZE;
+			zb.outlen = (nbuf * GOT_ZSTREAM_BUFSIZE) - *outlen;
 		}
 	} while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
 
@@ -251,6 +253,7 @@ got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen
 	size_t avail;
 	struct got_zstream_buf zb;
 	void *newbuf;
+	int nbuf = 1;
 
 	*outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
 	if (*outbuf == NULL)
@@ -267,8 +270,9 @@ got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen
 			goto done;
 		*outlen += avail;
 		if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
-			newbuf = reallocarray(*outbuf, 1,
-			    *outlen + GOT_ZSTREAM_BUFSIZE);
+			nbuf++;
+			newbuf = recallocarray(*outbuf, nbuf - 1, nbuf,
+			    GOT_ZSTREAM_BUFSIZE);
 			if (newbuf == NULL) {
 				err = got_error_from_errno();
 				free(*outbuf);
@@ -278,7 +282,7 @@ got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen
 			}
 			*outbuf = newbuf;
 			zb.outbuf = newbuf + *outlen;
-			zb.outlen = GOT_ZSTREAM_BUFSIZE;
+			zb.outlen = (nbuf * GOT_ZSTREAM_BUFSIZE) - *outlen;
 		}
 	} while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);