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.
This commit is contained in:
FRIGN 2015-03-27 14:49:48 +01:00
parent a516338581
commit 9144d51594
15 changed files with 22 additions and 21 deletions

2
comm.c
View File

@ -62,7 +62,7 @@ main(int argc, char *argv[])
for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) {
if (diff && i == (diff < 0)) if (diff && i == (diff < 0))
continue; continue;
if (getline(&line[i], &linelen[i], fp[i]) >= 0) if (getline(&line[i], &linelen[i], fp[i]) > 0)
continue; continue;
if (ferror(fp[i])) if (ferror(fp[i]))
eprintf("getline %s:", argv[i]); eprintf("getline %s:", argv[i]);

2
cron.c
View File

@ -304,7 +304,7 @@ loadentries(void)
return -1; return -1;
} }
for (y = 0; (len = getline(&line, &size, fp)) != -1; y++) { for (y = 0; (len = getline(&line, &size, fp)) > 0; y++) {
p = line; p = line;
if (line[0] == '#' || line[0] == '\n' || line[0] == '\0') if (line[0] == '#' || line[0] == '\n' || line[0] == '\0')
continue; continue;

2
cut.c
View File

@ -114,7 +114,7 @@ cut(FILE *fp, char *fname)
ssize_t len; ssize_t len;
Range *r; Range *r;
while ((len = getline(&buf, &size, fp)) >= 0) { while ((len = getline(&buf, &size, fp)) > 0) {
if (len && buf[len - 1] == '\n') if (len && buf[len - 1] == '\n')
buf[len - 1] = '\0'; buf[len - 1] = '\0';
if (mode == 'f' && !utfutf(buf, delim)) { if (mode == 'f' && !utfutf(buf, delim)) {

2
fold.c
View File

@ -55,7 +55,7 @@ fold(FILE *fp, const char *fname)
char *buf = NULL; char *buf = NULL;
size_t size = 0; size_t size = 0;
while (getline(&buf, &size, fp) >= 0) while (getline(&buf, &size, fp) > 0)
foldline(buf); foldline(buf);
if (ferror(fp)) if (ferror(fp))
eprintf("getline %s:", fname); eprintf("getline %s:", fname);

4
grep.c
View File

@ -87,7 +87,7 @@ addpatternfile(FILE *fp)
static size_t size = 0; static size_t size = 0;
ssize_t len = 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') if (len > 0 && buf[len - 1] == '\n')
buf[len - 1] = '\0'; buf[len - 1] = '\0';
addpattern(buf); addpattern(buf);
@ -106,7 +106,7 @@ grep(FILE *fp, const char *str)
struct pattern *pnode; struct pattern *pnode;
int match = NoMatch; 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. */ /* Remove the trailing newline if one is present. */
if (len && buf[len - 1] == '\n') if (len && buf[len - 1] == '\n')
buf[len - 1] = '\0'; buf[len - 1] = '\0';

2
head.c
View File

@ -12,7 +12,7 @@ head(FILE *fp, const char *fname, size_t n)
size_t i = 0, size = 0; size_t i = 0, size = 0;
ssize_t len; ssize_t len;
while (i < n && (len = getline(&buf, &size, fp)) >= 0) { while (i < n && (len = getline(&buf, &size, fp)) > 0) {
fputs(buf, stdout); fputs(buf, stdout);
i += (len && (buf[len - 1] == '\n')); i += (len && (buf[len - 1] == '\n'));
} }

View File

@ -45,7 +45,7 @@ mdchecklist(FILE *listfp, struct crypt_ops *ops, uint8_t *md, size_t sz,
int r; int r;
char *line = NULL, *file, *p; char *line = NULL, *file, *p;
while (getline(&line, &bufsiz, listfp) != -1) { while (getline(&line, &bufsiz, listfp) > 0) {
if (!(file = strstr(line, " "))) { if (!(file = strstr(line, " "))) {
(*formatsucks)++; (*formatsucks)++;
continue; continue;

View File

@ -13,7 +13,7 @@ getlines(FILE *fp, struct linebuf *b)
size_t size = 0, linelen; size_t size = 0, linelen;
ssize_t len; ssize_t len;
while ((len = getline(&line, &size, fp)) != -1) { while ((len = getline(&line, &size, fp)) > 0) {
if (++b->nlines > b->capacity) { if (++b->nlines > b->capacity) {
b->capacity += 512; b->capacity += 512;
nline = erealloc(b->lines, b->capacity * sizeof(*b->lines)); nline = erealloc(b->lines, b->capacity * sizeof(*b->lines));

View File

@ -70,7 +70,7 @@ main(int argc, char *argv[])
openlog(tag ? tag : getlogin(), logflags, 0); openlog(tag ? tag : getlogin(), logflags, 0);
if (!argc) { if (!argc) {
while (getline(&buf, &sz, stdin) >= 0) while (getline(&buf, &sz, stdin) > 0)
syslog(priority, "%s", buf); syslog(priority, "%s", buf);
if (ferror(stdin)) if (ferror(stdin))
eprintf("getline %s:", "<stdin>"); eprintf("getline %s:", "<stdin>");

2
nl.c
View File

@ -50,7 +50,7 @@ nl(const char *fname, FILE *fp)
int donumber, oldsection, section = 1, bl = 1; int donumber, oldsection, section = 1, bl = 1;
char *buf = NULL; char *buf = NULL;
while (getline(&buf, &size, fp) >= 0) { while (getline(&buf, &size, fp) > 0) {
donumber = 0; donumber = 0;
oldsection = section; oldsection = section;

2
sed.c
View File

@ -451,7 +451,7 @@ read_line(FILE *f, String *s)
if ((len = getline(&s->str, &s->cap, f)) < 0) { if ((len = getline(&s->str, &s->cap, f)) < 0) {
if (ferror(f)) if (ferror(f))
eprintf("getline failed\n"); eprintf("getline:");
return EOF; return EOF;
} }
if (s->str[--len] == '\n') if (s->str[--len] == '\n')

6
sort.c
View File

@ -63,9 +63,9 @@ check(FILE *fp)
{ {
static struct { char *buf; size_t size; } prev, cur, tmp; static struct { char *buf; size_t size; } prev, cur, tmp;
if (!prev.buf) if (!prev.buf && getline(&prev.buf, &prev.size, fp) < 0)
getline(&prev.buf, &prev.size, fp); eprintf("getline:");
while (getline(&cur.buf, &cur.size, fp) != -1) { while (getline(&cur.buf, &cur.size, fp) > 0) {
if (uflag > linecmp((const char **) &cur.buf, (const char **) &prev.buf)) { if (uflag > linecmp((const char **) &cur.buf, (const char **) &prev.buf)) {
if (!Cflag) if (!Cflag)
weprintf("disorder: %s", cur.buf); weprintf("disorder: %s", cur.buf);

6
tail.c
View File

@ -24,7 +24,7 @@ dropinit(FILE *fp, const char *str)
ssize_t len; ssize_t len;
if (mode == 'n') { 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') if (len > 0 && buf[len - 1] == '\n')
i++; i++;
} else { } else {
@ -46,7 +46,7 @@ taketail(FILE *fp, const char *str)
ring = ecalloc(num, sizeof *ring); ring = ecalloc(num, sizeof *ring);
size = ecalloc(num, sizeof *size); 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; i = j = (i + 1) % num;
} else { } else {
r = ecalloc(num, sizeof *r); r = ecalloc(num, sizeof *r);
@ -131,7 +131,7 @@ main(int argc, char *argv[])
continue; continue;
} }
for (tmp = NULL, tmpsize = 0;;) { for (tmp = NULL, tmpsize = 0;;) {
while (getline(&tmp, &tmpsize, fp) >= 0) { while (getline(&tmp, &tmpsize, fp) > 0) {
fputs(tmp, stdout); fputs(tmp, stdout);
fflush(stdout); fflush(stdout);
} }

2
uniq.c
View File

@ -76,7 +76,7 @@ uniq(FILE *fp, FILE *ofp)
size_t size = 0; size_t size = 0;
ssize_t len; ssize_t len;
while ((len = getline(&buf, &size, fp)) >= 0) while ((len = getline(&buf, &size, fp)) > 0)
uniqline(ofp, buf, (size_t)len); uniqline(ofp, buf, (size_t)len);
} }

View File

@ -166,7 +166,7 @@ uudecode(FILE *fp, FILE *outfp)
#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) ) #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) #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; p = bufb;
/* trim newlines */ /* trim newlines */
if (!len || bufb[len - 1] != '\n') if (!len || bufb[len - 1] != '\n')
@ -210,7 +210,8 @@ uudecode(FILE *fp, FILE *outfp)
eprintf("read error:"); eprintf("read error:");
} }
/* check for end or fail */ /* 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') if (len < 3 || strncmp(bufb, "end", 3) || bufb[3] != '\n')
eprintf("invalid uudecode footer \"end\" not found\n"); eprintf("invalid uudecode footer \"end\" not found\n");
free(bufb); free(bufb);