]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
utils/read_str_safe(): fix wrong behavior and bugs.
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>
Tue, 3 Jan 2012 16:22:05 +0000 (14:22 -0200)
committerGustavo Sverzut Barbieri <barbieri@profusion.mobi>
Tue, 3 Jan 2012 16:22:05 +0000 (14:22 -0200)
ouch, I did a mess in the original function, fix them:

 * on errors (read() < 0), continue reading after the done bytes, not
   at position 0.

 * read buflen - 1 bytes, so there is always room to store the
   trailing \0, as expected by user due behavior of snprintf(),
   fgets() and others.

libkmod/libkmod-util.c

index a86eda39c377ec2afc099d66127116106891708f..75e2fea7973e28efb5a3545eed4efbbdee3beb58 100644 (file)
@@ -205,17 +205,18 @@ inline void *memdup(const void *p, size_t n)
 }
 
 ssize_t read_str_safe(int fd, char *buf, size_t buflen) {
-       size_t todo = buflen;
-       size_t done;
+       size_t todo = buflen - 1;
+       size_t done = 0;
 
        do {
-               ssize_t r = read(fd, buf, todo);
+               ssize_t r = read(fd, buf + done, todo);
 
                if (r == 0)
                        break;
-               else if (r > 0)
+               else if (r > 0) {
                        todo -= r;
-               else {
+                       done += r;
+               } else {
                        if (errno == EAGAIN || errno == EWOULDBLOCK ||
                                errno == EINTR)
                                continue;
@@ -224,17 +225,7 @@ ssize_t read_str_safe(int fd, char *buf, size_t buflen) {
                }
        } while (todo > 0);
 
-       done = buflen - todo;
-
-       if (done == 0)
-               buf[0] = '\0';
-       else {
-               if (done < buflen)
-                       buf[done] = '\0';
-               else if (buf[done - 1] != '\0')
-                       return -ENOSPC;
-       }
-
+       buf[done] = '\0';
        return done;
 }