Fix a potential segfault, thanks to Fernando J. Pereda.

This commit is contained in:
Roy Marples 2008-05-19 13:16:53 +00:00
parent 148caecc7e
commit 775df18a70

View File

@ -59,24 +59,20 @@ ssize_t rc_getline(char **line, size_t *len, FILE *fp)
char *p; char *p;
size_t last = 0; size_t last = 0;
if (feof(fp)) while(!feof(fp)) {
return 0;
do {
if (*line == NULL || last != 0) { if (*line == NULL || last != 0) {
*len += BUFSIZ; *len += BUFSIZ;
*line = xrealloc(*line, *len); *line = realloc(*line, *len);
} }
p = *line + last; p = *line + last;
memset(p, 0, BUFSIZ); memset(p, 0, BUFSIZ);
fgets(p, BUFSIZ, fp); fgets(p, BUFSIZ, fp);
last += strlen(p); last += strlen(p);
} while (!feof(fp) && (*line)[last - 1] != '\n'); if (last && (*line)[last - 1] == '\n') {
/* Trim the trailing newline */
if (**line && (*line)[last - 1] == '\n')
(*line)[last - 1] = '\0'; (*line)[last - 1] = '\0';
break;
}
}
return last; return last;
} }
librc_hidden_def(rc_getline) librc_hidden_def(rc_getline)