sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 29649762b3602f4ce54ca8bb282524e5f1a92b92
parent e3cdcce2ec16ab69a0af94135266cf9c3bc8832c
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Fri,  8 May 2015 17:10:39 +0200

libutil/getlines: fix potential crash

linelen was uninitialized if for example:

$ > empty
$ sort ls.c empty

Diffstat:
Mlibutil/getlines.c | 4++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libutil/getlines.c b/libutil/getlines.c @@ -10,7 +10,7 @@ void getlines(FILE *fp, struct linebuf *b) { char *line = NULL; - size_t size = 0, linelen; + size_t size = 0, linelen = 0; ssize_t len; while ((len = getline(&line, &size, fp)) > 0) { @@ -22,7 +22,7 @@ getlines(FILE *fp, struct linebuf *b) b->lines[b->nlines - 1] = memcpy(emalloc(linelen + 1), line, linelen + 1); } free(line); - if (b->lines && b->nlines && b->lines[b->nlines - 1][linelen - 1] != '\n') { + if (b->lines && b->nlines && linelen && b->lines[b->nlines - 1][linelen - 1] != '\n') { b->lines[b->nlines - 1] = erealloc(b->lines[b->nlines - 1], linelen + 2); b->lines[b->nlines - 1][linelen] = '\n'; b->lines[b->nlines - 1][linelen + 1] = '\0';