Skip to contentSkip to conversation

erofs-utils: tar: handle gzread errors

PATCH New Vansh Choudhary 2 participants 2 messages Mar 30 – Mar 31 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: tar: handle gzread errors

Treat gzread() errors as I/O failures before updating the stream buffer state.

Without that, a negative gzread() result could underflow ios->tail and leave the reader in a corrupted state.

---
 lib/tar.c | 8 ++++++++
 1 file changed, 8 insertions(+)
lib/tar.c +8 -0
index 39e2321..871779a 100644
@@ -174,6 +174,14 @@ int erofs_iostream_read(struct erofs_iostream *ios, void **buf, u64 bytes)
 #if defined(HAVE_ZLIB)
 			ret = gzread(ios->handler, ios->buffer + rabytes,
 				     ios->bufsize - rabytes);
+			if (ret < 0) {
+				int errnum;
+				const char *errstr;
+
+				errstr = gzerror(ios->handler, &errnum);
+				erofs_err("failed to gzread: %s", errstr);
+				return -EIO;
+			}
 			if (!ret) {
 				int errnum;
 				const char *errstr;
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/1 erofs-utils: tar: handle gzread errors

From: Vansh Choudhary <[email protected]>

Treat gzread() errors as I/O failures before updating the stream buffer state.

Without that, a negative gzread() result could underflow ios->tail and leave the reader in a corrupted state.

---
v2:
- some cleanups.

I will apply this one.

 lib/tar.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
lib/tar.c +5 -4
index eca29f54c06f..0fe9853dfb16 100644
@@ -170,16 +170,17 @@ int erofs_iostream_read(struct erofs_iostream *ios, void **buf, u64 bytes)
 #if defined(HAVE_ZLIB)
 			ret = gzread(ios->handler, ios->buffer + rabytes,
 				     ios->bufsize - rabytes);
-			if (!ret) {
-				int errnum;
+			if (ret <= 0) {
 				const char *errstr;
+				int errnum;
 
 				errstr = gzerror(ios->handler, &errnum);
-				if (errnum != Z_STREAM_END) {
+				if (!ret && errnum == Z_STREAM_END) {
+					ios->feof = true;
+				} else {
 					erofs_err("failed to gzread: %s", errstr);
 					return -EIO;
 				}
-				ios->feof = true;
 			}
 			ios->tail += ret;
 #else
Signature · 1 line
2.43.5