On my Mac I saw a CPU spin which looked like this:
```
Call graph:
2192 Thread_132504 DispatchQueue_1: com.apple.main-thread (serial)
2192 start (in dyld) + 6992 [0x185bcbda4]
2192 main (in mkfs.erofs) + 7916 [0x10253a6d0]
2192 tarerofs_parse_tar (in mkfs.erofs) + 5492 [0x102551d48]
2187 tarerofs_write_file_data (in mkfs.erofs) + 140 [0x102551fe0]
+ 2187 write (in libsystem_kernel.dylib) + 8 [0x185f47834]
4 tarerofs_write_file_data (in mkfs.erofs) + 116 [0x102551fc8]
+ 4 erofs_iostream_read (in mkfs.erofs) + 16,36,...
[0x10254fa28,0x10254fa3c,...]
1 tarerofs_write_file_data (in mkfs.erofs) + 140 [0x102551fe0]
```
The input stream was closed prematurely, so the reads returned 0 (EOF),
which wasn't considered an error.
Treat return of 0 (EOF) as an error.
Reproduce by:
```
dd if=/dev/zero bs=1024 count=4 2>/dev/null > /tmp/testfile
COPYFILE_DISABLE=1 tar cf - -C /tmp testfile | head -c 2048 > /tmp/truncated.tar
./mkfs/mkfs.erofs --tar=f output.erofs < /tmp/truncated.tar
```
Before the patch this will hang, after it should fail as expected.
(COPYFILE_DISABLE tells mac to avoid putting extra stuff in the tar)
Closes: https://github.com/erofs/erofs-utils/issues/43
Signed-off-by: David Scott <[email protected]>
---
lib/tar.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/lib/tar.c b/lib/tar.c
index 178f843..57c6fee 100644
--- a/lib/tar.c
+++ b/lib/tar.c
@@ -638,8 +638,11 @@ static int tarerofs_write_uncompressed_file(struct
erofs_inode *inode,
for (pos = 0; pos < inode->i_size; pos += ret) {
ret = erofs_iostream_read(&tar->ios, &buf, inode->i_size - pos);
- if (ret < 0)
+ if (ret <= 0) {
+ if (!ret)
+ ret = -EIO;
break;
+ }
if (erofs_dev_write(sbi, buf,
erofs_pos(sbi, inode->u.i_blkaddr) + pos,
ret)) {
@@ -649,6 +652,8 @@ static int tarerofs_write_uncompressed_file(struct
erofs_inode *inode,
}
inode->idata_size = 0;
inode->datasource = EROFS_INODE_DATA_SOURCE_NONE;
+ if (ret < 0)
+ return ret;
return 0;
}
@@ -673,8 +678,11 @@ static int tarerofs_write_file_data(struct erofs_inode
*inode,
for (j = inode->i_size; j; ) {
nread = erofs_iostream_read(&tar->ios, &buf, j);
- if (nread < 0)
+ if (nread <= 0) {
+ if (!nread)
+ nread = -EIO;
break;
+ }
if (pwrite(fd, buf, nread, off) != nread) {
nread = -EIO;
break;
@@ -684,6 +692,8 @@ static int tarerofs_write_file_data(struct erofs_inode
*inode,
}
erofs_diskbuf_commit(inode->i_diskbuf, inode->i_size);
inode->datasource = EROFS_INODE_DATA_SOURCE_DISKBUF;
+ if (nread < 0)
+ return nread;
return 0;
}
--
2.43.0