fix single-byte overflow of malloc'd buffer in getdelim
authorRich Felker <dalias@aerifal.cx>
Sun, 25 Oct 2015 02:42:10 +0000 (22:42 -0400)
committerRich Felker <dalias@aerifal.cx>
Sun, 25 Oct 2015 02:42:10 +0000 (22:42 -0400)
the buffer enlargement logic here accounted for the terminating null
byte, but not for the possibility of hitting the delimiter in the
buffer-refill code path that uses getc_unlocked, in which case two
additional bytes (the delimiter and the null termination) are written
without another chance to enlarge the buffer.

this patch and the corresponding bug report are by Felix Janda.

src/stdio/getdelim.c

index a88c393..3077490 100644 (file)
@@ -27,7 +27,7 @@ ssize_t getdelim(char **restrict s, size_t *restrict n, int delim, FILE *restric
        for (;;) {
                z = memchr(f->rpos, delim, f->rend - f->rpos);
                k = z ? z - f->rpos + 1 : f->rend - f->rpos;
-               if (i+k >= *n) {
+               if (i+k+1 >= *n) {
                        if (k >= SIZE_MAX/2-i) goto oom;
                        *n = i+k+2;
                        if (*n < SIZE_MAX/4) *n *= 2;