Eric Blake wrote: > And all I did was make getndelim2 smarter to process data in blocks > instead of bytes
I'm adding these comments. Otherwise, it is impossible to understand why there is no buffer overrun at the end (when the terminating '\0' is stored). *** lib/getndelim2.c.orig 2008-05-01 16:41:09.000000000 +0200 --- lib/getndelim2.c 2008-05-01 16:40:13.000000000 +0200 *************** *** 105,111 **** while (!done) { ! /* Here always ptr + size == read_pos + nbytes_avail. */ int c IF_LINT (= 0); const char *buffer; --- 105,112 ---- while (!done) { ! /* Here always ptr + size == read_pos + nbytes_avail. ! Also nbytes_avail > 0 || size < nmax. */ int c IF_LINT (= 0); const char *buffer; *************** *** 171,176 **** --- 172,180 ---- read_pos = size - nbytes_avail + ptr; } + /* Here, if size < nmax, nbytes_avail >= buffer_len + 1. + If size == nmax, nbytes_avail > 0. */ + if (1 < nbytes_avail) { size_t copy_len = nbytes_avail - 1; *************** *** 183,188 **** --- 187,195 ---- read_pos += copy_len; nbytes_avail -= copy_len; } + + /* Here still nbytes_avail > 0. */ + if (buffer && freadseek (stream, buffer_len)) goto unlock_done; }