From 775df18a70cacb4857be43335c13d2c7a8013d6a Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Mon, 19 May 2008 13:16:53 +0000 Subject: Fix a potential segfault, thanks to Fernando J. Pereda. --- src/librc/librc-misc.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'src/librc') 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) -- cgit v1.2.3