[hackers] [sbase] Check getline()-return-values properly || FRIGN

From: <git_AT_suckless.org>
Date: Sat, 28 Mar 2015 22:03:52 +0100 (CET)

commit 9144d51594d8b35f01da352bed0282afbbc39a24
Author: FRIGN <dev_AT_frign.de>
Date: Fri Mar 27 14:49:48 2015 +0100

    Check getline()-return-values properly
    
    It's not useful when 0 is returned anyway, so be sure that we have a
    string with length > 0, this also solves some indexing-gotchas like
    "len - 1" and so on.
    Also, add checked getline()'s whenever it has been forgotten and
    clean up the error-messages.

diff --git a/comm.c b/comm.c
index fec5723..84c9fb3 100644
--- a/comm.c
+++ b/comm.c
_AT_@ -62,7 +62,7 @@ main(int argc, char *argv[])
                 for (i = 0; i < 2; i++) {
                         if (diff && i == (diff < 0))
                                 continue;
- if (getline(&line[i], &linelen[i], fp[i]) >= 0)
+ if (getline(&line[i], &linelen[i], fp[i]) > 0)
                                 continue;
                         if (ferror(fp[i]))
                                 eprintf("getline %s:", argv[i]);
diff --git a/cron.c b/cron.c
index b919c66..cd03b9b 100644
--- a/cron.c
+++ b/cron.c
_AT_@ -304,7 +304,7 @@ loadentries(void)
                 return -1;
         }
 
- for (y = 0; (len = getline(&line, &size, fp)) != -1; y++) {
+ for (y = 0; (len = getline(&line, &size, fp)) > 0; y++) {
                 p = line;
                 if (line[0] == '#' || line[0] == '\n' || line[0] == '\0')
                         continue;
diff --git a/cut.c b/cut.c
index 92da563..7c806af 100644
--- a/cut.c
+++ b/cut.c
_AT_@ -114,7 +114,7 @@ cut(FILE *fp, char *fname)
         ssize_t len;
         Range *r;
 
- while ((len = getline(&buf, &size, fp)) >= 0) {
+ while ((len = getline(&buf, &size, fp)) > 0) {
                 if (len && buf[len - 1] == '\n')
                         buf[len - 1] = '\0';
                 if (mode == 'f' && !utfutf(buf, delim)) {
diff --git a/fold.c b/fold.c
index c176497..6a52466 100644
--- a/fold.c
+++ b/fold.c
_AT_@ -55,7 +55,7 @@ fold(FILE *fp, const char *fname)
         char *buf = NULL;
         size_t size = 0;
 
- while (getline(&buf, &size, fp) >= 0)
+ while (getline(&buf, &size, fp) > 0)
                 foldline(buf);
         if (ferror(fp))
                 eprintf("getline %s:", fname);
diff --git a/grep.c b/grep.c
index e41eb57..db7539c 100644
--- a/grep.c
+++ b/grep.c
_AT_@ -87,7 +87,7 @@ addpatternfile(FILE *fp)
         static size_t size = 0;
         ssize_t len = 0;
 
- while ((len = getline(&buf, &size, fp)) != -1) {
+ while ((len = getline(&buf, &size, fp)) > 0) {
                 if (len > 0 && buf[len - 1] == '\n')
                         buf[len - 1] = '\0';
                 addpattern(buf);
_AT_@ -106,7 +106,7 @@ grep(FILE *fp, const char *str)
         struct pattern *pnode;
         int match = NoMatch;
 
- for (n = 1; (len = getline(&buf, &size, fp)) != -1; n++) {
+ for (n = 1; (len = getline(&buf, &size, fp)) > 0; n++) {
                 /* Remove the trailing newline if one is present. */
                 if (len && buf[len - 1] == '\n')
                         buf[len - 1] = '\0';
diff --git a/head.c b/head.c
index 6c1d937..5137729 100644
--- a/head.c
+++ b/head.c
_AT_@ -12,7 +12,7 @@ head(FILE *fp, const char *fname, size_t n)
         size_t i = 0, size = 0;
         ssize_t len;
 
- while (i < n && (len = getline(&buf, &size, fp)) >= 0) {
+ while (i < n && (len = getline(&buf, &size, fp)) > 0) {
                 fputs(buf, stdout);
                 i += (len && (buf[len - 1] == '\n'));
         }
diff --git a/libutil/crypt.c b/libutil/crypt.c
index 91f80e6..7f76047 100644
--- a/libutil/crypt.c
+++ b/libutil/crypt.c
_AT_@ -45,7 +45,7 @@ mdchecklist(FILE *listfp, struct crypt_ops *ops, uint8_t *md, size_t sz,
         int r;
         char *line = NULL, *file, *p;
 
- while (getline(&line, &bufsiz, listfp) != -1) {
+ while (getline(&line, &bufsiz, listfp) > 0) {
                 if (!(file = strstr(line, " "))) {
                         (*formatsucks)++;
                         continue;
diff --git a/libutil/getlines.c b/libutil/getlines.c
index 30eb721..6746c0f 100644
--- a/libutil/getlines.c
+++ b/libutil/getlines.c
_AT_@ -13,7 +13,7 @@ getlines(FILE *fp, struct linebuf *b)
         size_t size = 0, linelen;
         ssize_t len;
 
- while ((len = getline(&line, &size, fp)) != -1) {
+ while ((len = getline(&line, &size, fp)) > 0) {
                 if (++b->nlines > b->capacity) {
                         b->capacity += 512;
                         nline = erealloc(b->lines, b->capacity * sizeof(*b->lines));
diff --git a/logger.c b/logger.c
index 80dcdc7..a4c240e 100644
--- a/logger.c
+++ b/logger.c
_AT_@ -70,7 +70,7 @@ main(int argc, char *argv[])
         openlog(tag ? tag : getlogin(), logflags, 0);
 
         if (!argc) {
- while (getline(&buf, &sz, stdin) >= 0)
+ while (getline(&buf, &sz, stdin) > 0)
                         syslog(priority, "%s", buf);
                 if (ferror(stdin))
                         eprintf("getline %s:", "<stdin>");
diff --git a/nl.c b/nl.c
index 3a1e041..e78c5dc 100644
--- a/nl.c
+++ b/nl.c
_AT_@ -50,7 +50,7 @@ nl(const char *fname, FILE *fp)
         int donumber, oldsection, section = 1, bl = 1;
         char *buf = NULL;
 
- while (getline(&buf, &size, fp) >= 0) {
+ while (getline(&buf, &size, fp) > 0) {
                 donumber = 0;
                 oldsection = section;
 
diff --git a/sed.c b/sed.c
index 0c7440e..86206b1 100644
--- a/sed.c
+++ b/sed.c
_AT_@ -451,7 +451,7 @@ read_line(FILE *f, String *s)
 
         if ((len = getline(&s->str, &s->cap, f)) < 0) {
                 if (ferror(f))
- eprintf("getline failed\n");
+ eprintf("getline:");
                 return EOF;
         }
         if (s->str[--len] == '\n')
diff --git a/sort.c b/sort.c
index 87d5d3b..58e0b91 100644
--- a/sort.c
+++ b/sort.c
_AT_@ -63,9 +63,9 @@ check(FILE *fp)
 {
         static struct { char *buf; size_t size; } prev, cur, tmp;
 
- if (!prev.buf)
- getline(&prev.buf, &prev.size, fp);
- while (getline(&cur.buf, &cur.size, fp) != -1) {
+ if (!prev.buf && getline(&prev.buf, &prev.size, fp) < 0)
+ eprintf("getline:");
+ while (getline(&cur.buf, &cur.size, fp) > 0) {
                 if (uflag > linecmp((const char **) &cur.buf, (const char **) &prev.buf)) {
                         if (!Cflag)
                                 weprintf("disorder: %s", cur.buf);
diff --git a/tail.c b/tail.c
index 2e64ce5..6d02914 100644
--- a/tail.c
+++ b/tail.c
_AT_@ -24,7 +24,7 @@ dropinit(FILE *fp, const char *str)
         ssize_t len;
 
         if (mode == 'n') {
- while (i < num && (len = getline(&buf, &size, fp)) >= 0)
+ while (i < num && (len = getline(&buf, &size, fp)) > 0)
                         if (len > 0 && buf[len - 1] == '\n')
                                 i++;
         } else {
_AT_@ -46,7 +46,7 @@ taketail(FILE *fp, const char *str)
                 ring = ecalloc(num, sizeof *ring);
                 size = ecalloc(num, sizeof *size);
 
- for (i = j = 0; getline(&ring[i], &size[i], fp) != -1; )
+ for (i = j = 0; getline(&ring[i], &size[i], fp) > 0; )
                         i = j = (i + 1) % num;
         } else {
                 r = ecalloc(num, sizeof *r);
_AT_@ -131,7 +131,7 @@ main(int argc, char *argv[])
                                 continue;
                         }
                         for (tmp = NULL, tmpsize = 0;;) {
- while (getline(&tmp, &tmpsize, fp) >= 0) {
+ while (getline(&tmp, &tmpsize, fp) > 0) {
                                         fputs(tmp, stdout);
                                         fflush(stdout);
                                 }
diff --git a/uniq.c b/uniq.c
index 6d1e139..344e158 100644
--- a/uniq.c
+++ b/uniq.c
_AT_@ -76,7 +76,7 @@ uniq(FILE *fp, FILE *ofp)
         size_t size = 0;
         ssize_t len;
 
- while ((len = getline(&buf, &size, fp)) >= 0)
+ while ((len = getline(&buf, &size, fp)) > 0)
                 uniqline(ofp, buf, (size_t)len);
 }
 
diff --git a/uudecode.c b/uudecode.c
index c722ab9..7c18af6 100644
--- a/uudecode.c
+++ b/uudecode.c
_AT_@ -166,7 +166,7 @@ uudecode(FILE *fp, FILE *outfp)
 #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
 #define OUT_OF_RANGE(c) eprintf("character %c out of range: [%d-%d]\n", (c), 1 + ' ', 077 + ' ' + 1)
 
- while ((len = getline(&bufb, &n, fp)) != -1) {
+ while ((len = getline(&bufb, &n, fp)) > 0) {
                 p = bufb;
                 /* trim newlines */
                 if (!len || bufb[len - 1] != '\n')
_AT_@ -210,7 +210,8 @@ uudecode(FILE *fp, FILE *outfp)
                         eprintf("read error:");
         }
         /* check for end or fail */
- len = getline(&bufb, &n, fp);
+ if ((len = getline(&bufb, &n, fp)) < 0)
+ eprintf("getline:");
         if (len < 3 || strncmp(bufb, "end", 3) || bufb[3] != '\n')
                 eprintf("invalid uudecode footer \"end\" not found\n");
         free(bufb);
Received on Sat Mar 28 2015 - 22:03:52 CET

This archive was generated by hypermail 2.3.0 : Sat Mar 28 2015 - 22:12:20 CET