[Toybox] [PATCH 1/2] Handle large read and write lengths.

Samanta Navarro ferivoz at riseup.net
Sat Aug 7 05:11:13 PDT 2021


The functions readall and writeall can return an error value by mistake
if more than 2 GB of data are read or written.

This happens because int and ssize_t are of different sizes on 64 bit
architectures. Using ssize_t resolves the issue because read and write
return ssize_t already.
---
 lib/lib.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/lib.c b/lib/lib.c
index 49fd5dd2..d4397327 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -112,7 +112,7 @@ ssize_t readall(int fd, void *buf, size_t len)
   size_t count = 0;
 
   while (count<len) {
-    int i = read(fd, (char *)buf+count, len-count);
+    ssize_t i = read(fd, (char *)buf+count, len-count);
     if (!i) break;
     if (i<0) return i;
     count += i;
@@ -127,7 +127,7 @@ ssize_t writeall(int fd, void *buf, size_t len)
   size_t count = 0;
 
   while (count<len) {
-    int i = write(fd, count+(char *)buf, len-count);
+    ssize_t i = write(fd, count+(char *)buf, len-count);
     if (i<1) return i;
     count += i;
   }
-- 
2.32.0




More information about the Toybox mailing list