From: Gustavo Sverzut Barbieri Date: Tue, 3 Jan 2012 16:22:05 +0000 (-0200) Subject: utils/read_str_safe(): fix wrong behavior and bugs. X-Git-Tag: v3~20 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=671799415717ee6d10df48ce03d60c5d75cded9f;p=thirdparty%2Fkmod.git utils/read_str_safe(): fix wrong behavior and bugs. 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. --- diff --git a/libkmod/libkmod-util.c b/libkmod/libkmod-util.c index a86eda39..75e2fea7 100644 --- a/libkmod/libkmod-util.c +++ b/libkmod/libkmod-util.c @@ -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; }