Skip to content

Commit e9c7aa3

Browse files
committed
fileio: fix free buffer and off-by-one in readline
1 parent df98916 commit e9c7aa3

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/fileio/scan.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ static ssize_t readline(char** lineptr, size_t* n, FILE* stream) {
6060
}
6161
p = bufptr;
6262
while (c != EOF) {
63-
if ((ssize_t)(p - bufptr)+1 > (ssize_t)(size)) {
64-
size_t offset = p - bufptr; // save offset
65-
size = size + 128;
66-
char * new_buf = realloc(bufptr, size);
63+
if ((size_t)(p - bufptr) >= size - 1) {
64+
size = size + 128;
65+
char* new_buf = realloc(bufptr, size);
6766
if (new_buf == NULL) {
67+
free(bufptr);
6868
return -1;
6969
}
70-
bufptr = new_buf;
71-
p = bufptr + offset; // recalculate p using the saved offset
70+
p = new_buf + (p - bufptr);
71+
bufptr = new_buf;
7272
}
7373
*p++ = c;
7474
if (c == '\n') {

0 commit comments

Comments
 (0)