Fix a potential segfault, thanks to Fernando J. Pereda.
This commit is contained in:
parent
148caecc7e
commit
775df18a70
@ -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') {
|
||||||
|
(*line)[last - 1] = '\0';
|
||||||
/* Trim the trailing newline */
|
break;
|
||||||
if (**line && (*line)[last - 1] == '\n')
|
}
|
||||||
(*line)[last - 1] = '\0';
|
}
|
||||||
|
|
||||||
return last;
|
return last;
|
||||||
}
|
}
|
||||||
librc_hidden_def(rc_getline)
|
librc_hidden_def(rc_getline)
|
||||||
|
Loading…
Reference in New Issue
Block a user