Skip to contentSkip to conversation

erofs-utils: lib: fix EINTR mishandling in erofs_io_read()

PATCH In review Ajay Rajera 2 participants 2 messages Mar 28 – Mar 29 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: lib: fix EINTR mishandling in erofs_io_read()

When read() is interrupted by a signal and returns -1 with errno == EINTR, erofs_io_read() falls through without zeroing ret. This causes bytes -= ret and i += ret to execute with ret == -1, corrupting the byte counter (bytes wraps around since it is size_t) and the offset. This can lead to incorrect reads or infinite loops.

Fix this by setting ret to 0 on EINTR before falling through, matching the existing pattern in __erofs_io_write() and erofs_io_pread().

Also fix inconsistent whitespace (spaces instead of tabs) on the closing brace and return statement.

---
 lib/io.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/io.c +3 -2
index 0c5eb2c..dd5e304 100644
@@ -551,11 +551,12 @@ ssize_t erofs_io_read(struct erofs_vfile *vf, void *buf, size_t bytes)
 					  strerror(errno));
 				return -errno;
 			}
+			ret = 0;
 		}
 		bytes -= ret;
 		i += ret;
-        }
-        return i;
+	}
+	return i;
 }
 
 ssize_t erofs_io_write(struct erofs_vfile *vf, void *buf, size_t len)
Signature · 1 line
2.51.0.windows.1
Reviewed-by on v1 1/1
#1 lore raw

Subject: Re: [PATCH] erofs-utils: lib: fix EINTR mishandling in erofs_io_read()

Quoted 2 lines · Ajay Rajera

Fix this by setting ret to 0 on EINTR before falling through, matching the existing pattern in __erofs_io_write() and erofs_io_pread().

Hi Ajay,

The fix is correct. Placing ret = 0 after the inner if-else is safe because the else branch always returns, so the assignment is only reachable on EINTR. It correctly handles the partial-read case too:
After a short read advances i and decrements bytes, a subsequent EINTR leaves both unchanged via ret = 0 and the retry continues from the right buffer position.

Quoted 43 lines · Ajay Rajera

When read() is interrupted by a signal and returns -1 with errno == EINTR, erofs_io_read() falls through without zeroing ret. This causes bytes -= ret and i += ret to execute with ret == -1, corrupting the byte counter (bytes wraps around since it is size_t) and the offset. This can lead to incorrect reads or infinite loops.

Fix this by setting ret to 0 on EINTR before falling through, matching the existing pattern in __erofs_io_write() and erofs_io_pread().

Also fix inconsistent whitespace (spaces instead of tabs) on the closing brace and return statement.

---
 lib/io.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/io.c +3 -3
index 0c5eb2c..dd5e304 100644
@@ -551,11 +551,12 @@ ssize_t erofs_io_read(struct erofs_vfile *vf, void *buf, size_t bytes)
                                          strerror(errno));
                                return -errno;
                        }
+                       ret = 0;
                }
                bytes -= ret;
                i += ret;
-        }
-        return i;
+       }
+       return i;
 }
 
 ssize_t erofs_io_write(struct erofs_vfile *vf, void *buf, size_t len)
--

2.51.0.windows.1