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:
parent
a516338581
commit
9144d51594
2
comm.c
2
comm.c
|
@ -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]);
|
||||
|
|
2
cron.c
2
cron.c
|
@ -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;
|
||||
|
|
2
cut.c
2
cut.c
|
@ -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)) {
|
||||
|
|
2
fold.c
2
fold.c
|
@ -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);
|
||||
|
|
4
grep.c
4
grep.c
|
@ -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);
|
||||
|
@ -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';
|
||||
|
|
2
head.c
2
head.c
|
@ -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'));
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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));
|
||||
|
|
2
logger.c
2
logger.c
|
@ -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>");
|
||||
|
|
2
nl.c
2
nl.c
|
@ -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;
|
||||
|
||||
|
|
2
sed.c
2
sed.c
|
@ -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')
|
||||
|
|
6
sort.c
6
sort.c
|
@ -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);
|
||||
|
|
6
tail.c
6
tail.c
|
@ -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 {
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
2
uniq.c
2
uniq.c
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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')
|
||||
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user