diff options
author | Roy Marples <roy@marples.name> | 2008-05-19 13:16:53 +0000 |
---|---|---|
committer | Roy Marples <roy@marples.name> | 2008-05-19 13:16:53 +0000 |
commit | 775df18a70cacb4857be43335c13d2c7a8013d6a (patch) | |
tree | 18b473e9fdd80f084a4708cb492ce235c4231c86 /src | |
parent | 148caecc7e96f54245269c10bfe0536220602540 (diff) |
Fix a potential segfault, thanks to Fernando J. Pereda.
Diffstat (limited to 'src')
-rw-r--r-- | src/librc/librc-misc.c | 18 |
1 files changed, 7 insertions, 11 deletions
diff --git a/src/librc/librc-misc.c b/src/librc/librc-misc.c index c9fa9659..1ae3a283 100644 --- a/src/librc/librc-misc.c +++ b/src/librc/librc-misc.c @@ -59,24 +59,20 @@ ssize_t rc_getline(char **line, size_t *len, FILE *fp) char *p; size_t last = 0; - if (feof(fp)) - return 0; - - do { + while(!feof(fp)) { if (*line == NULL || last != 0) { *len += BUFSIZ; - *line = xrealloc(*line, *len); + *line = realloc(*line, *len); } p = *line + last; memset(p, 0, BUFSIZ); fgets(p, BUFSIZ, fp); last += strlen(p); - } while (!feof(fp) && (*line)[last - 1] != '\n'); - - /* Trim the trailing newline */ - if (**line && (*line)[last - 1] == '\n') - (*line)[last - 1] = '\0'; - + if (last && (*line)[last - 1] == '\n') { + (*line)[last - 1] = '\0'; + break; + } + } return last; } librc_hidden_def(rc_getline) |