Commit 744d93265df30ca836528ad12d5c1d0d980570a9

Stefan Sperling 2017-12-01T22:38:52

fix size calculations

diff --git a/lib/object.c b/lib/object.c
index 779aab7..5274de4 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -204,17 +204,16 @@ read_object_header(struct got_object **obj, struct got_repository *repo,
 	FILE *f;
 	struct got_zstream_buf zb;
 	char *buf;
-	size_t totalsz;
+	size_t len;
 	const size_t zbsize = 64;
-	size_t outlen;
+	size_t outlen, totlen;
 	int i, ret;
 
 	f = fopen(path, "rb");
 	if (f == NULL)
 		return got_error(GOT_ERR_BAD_PATH);
 
-	totalsz = zbsize;
-	buf = calloc(totalsz, sizeof(char));
+	buf = calloc(zbsize, sizeof(char));
 	if (buf == NULL)
 		return got_error(GOT_ERR_NO_MEM);
 
@@ -225,19 +224,19 @@ read_object_header(struct got_object **obj, struct got_repository *repo,
 	}
 
 	i = 0;
+	totlen = 0;
 	do {
 		err = inflate_read(&zb, f, &outlen);
 		if (err)
 			goto done;
-		if (strchr(zb.outbuf, '\0') == NULL) {
+		if (strchr(zb.outbuf, '\0') == NULL)
 			buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
-			totalsz += zbsize;
-		}
-		memcpy(buf, zb.outbuf, zbsize);
+		memcpy(buf, zb.outbuf, outlen);
+		totlen += outlen;
 		i++;
 	} while (strchr(zb.outbuf, '\0') == NULL);
 
-	err = parse_object_header(obj, buf, totalsz);
+	err = parse_object_header(obj, buf, totlen);
 done:
 	inflate_end(&zb);
 	fclose(f);