[hackers] [sbase] libutil/getlines: fix potential crash || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Fri, 8 May 2015 17:38:11 +0200 (CEST)

commit 29649762b3602f4ce54ca8bb282524e5f1a92b92
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Fri May 8 17:10:39 2015 +0200

    libutil/getlines: fix potential crash
    
    linelen was uninitialized if for example:
    
    $ > empty
    $ sort ls.c empty

diff --git a/libutil/getlines.c b/libutil/getlines.c
index 3a4d0f4..9842d2b 100644
--- a/libutil/getlines.c
+++ b/libutil/getlines.c
_AT_@ -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) {
_AT_@ -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';
Received on Fri May 08 2015 - 17:38:11 CEST

This archive was generated by hypermail 2.3.0 : Fri May 08 2015 - 17:48:15 CEST