File position should be fixed to aligned with the chunk boundary.
Otherwise, incorrect data could be read.

Fixes: 7c49e8b195ad ("erofs-utils: support chunk-based sparse files")
Signed-off-by: Gao Xiang <[email protected]>
---
 lib/blobchunk.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lib/blobchunk.c b/lib/blobchunk.c
index 44541d5..c0df2f7 100644
--- a/lib/blobchunk.c
+++ b/lib/blobchunk.c
@@ -228,8 +228,12 @@ int erofs_blob_write_chunked_file(struct erofs_inode 
*inode, int fd)
                                offset = pos;
                        else
                                offset = ((pos >> chunkbits) + 1) << chunkbits;
-               } else {
+               } else if (offset != (offset & ~(chunksize - 1))) {
                        offset &= ~(chunksize - 1);
+                       if (lseek(fd, offset, SEEK_SET) != offset) {
+                               ret = -EIO;
+                               goto err;
+                       }
                }
 
                if (offset > pos) {
-- 
2.24.4

Reply via email to