libbb: Converted safe_read to safe_write format
authorMartin Lewis <martin.lewis.x84@gmail.com>
Sun, 15 Sep 2019 16:13:28 +0000 (18:13 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Wed, 9 Oct 2019 12:35:55 +0000 (14:35 +0200)
Changed safe_read to be symmetrical to safe_write, it shall
never return EINTR because it calls read multiple times,
the error is considered transient.

function                                             old     new   delta
safe_read                                             44      57     +13

Signed-off-by: Martin Lewis <martin.lewis.x84@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
libbb/read.c

index 5906bc225dc62dde00025cf77ffde09061acf2a2..a342506a8e738699f0f231c854e8b2f2be365162 100644 (file)
@@ -12,9 +12,17 @@ ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count)
 {
        ssize_t n;
 
-       do {
+       for (;;) {
                n = read(fd, buf, count);
-       } while (n < 0 && errno == EINTR);
+               if (n >= 0 || errno != EINTR)
+                       break;
+               /* Some callers set errno=0, are upset when they see EINTR.
+                * Returning EINTR is wrong since we retry read(),
+                * the "error" was transient.
+                */
+               errno = 0;
+               /* repeat the read() */
+       }
 
        return n;
 }