[PATCH 2/2] uncompress: fix prebuffering

Sascha Hauer s.hauer at pengutronix.de
Thu May 22 11:59:05 PDT 2025


When uncompressing from a fill function we prebuffer the first 32 bytes
in order to detect the filetype. This prebuffer is then fed back into
the input stream in uncompress_fill().

This currently fails when uncompress_fill() crosses the prebuffered
size and continues reading from the original fill function. Advance
the buffer in this case to append the data to the buffer instead of
overwriting the data we already copied from the prebuffer.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 lib/uncompress.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lib/uncompress.c b/lib/uncompress.c
index c284bcc6ba5649419a226af14a78f42142cf5e74..6a4e5a34df8a69d22ae51b9fb7423f7a052ef480 100644
--- a/lib/uncompress.c
+++ b/lib/uncompress.c
@@ -45,6 +45,7 @@ static long uncompress_fill(void *buf, unsigned long len)
 		int now = min(len, uncompress_size);
 
 		memcpy(buf, uncompress_buf, now);
+		uncompress_buf += now;
 		uncompress_size -= now;
 		len -= now;
 		total = now;
@@ -77,17 +78,17 @@ int uncompress(unsigned char *inbuf, long len,
             void(*error)(char *x));
 	int ret;
 	char *err;
+	void *uncompress_buf_free = NULL;
 
 	if (inbuf) {
 		ft = file_detect_compression_type(inbuf, len);
-		uncompress_buf = NULL;
 		uncompress_size = 0;
 	} else {
 		if (!fill)
 			return -EINVAL;
 
 		uncompress_fill_fn = fill;
-		uncompress_buf = xzalloc(32);
+		uncompress_buf_free = uncompress_buf = xzalloc(32);
 		uncompress_size = 32;
 
 		ret = fill(uncompress_buf, 32);
@@ -142,7 +143,7 @@ int uncompress(unsigned char *inbuf, long len,
 	ret = compfn(inbuf, len, fill ? uncompress_fill : NULL,
 			flush, output, pos, error_fn);
 err:
-	free(uncompress_buf);
+	free(uncompress_buf_free);
 
 	return ret;
 }

-- 
2.39.5




More information about the barebox mailing list