From 50b19aeb57b413e29e1eff28311433ea7d78f4a3 Mon Sep 17 00:00:00 2001 From: John Marino Date: Wed, 7 Nov 2012 21:15:35 +0100 Subject: [PATCH] getline(3): Fix segfault caused by NULL pointer This fixes a segfault seen with pkg's audit function, and effectively syncs getdelim with FreeBSD. Taken-from: FreeBSD SVN 197752 (04 OCT 2009) - Tolerate applications that pass a NULL pointer for the buffer and claim that the capacity of the buffer is nonzero. - If an application passes in a non-NULL buffer pointer and claims the buffer has zero capacity, we should free (well, realloc) it anyway. It could have been obtained from malloc(0), so failing to free it would be a small memory leak. --- lib/libc/stdio/getdelim.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/libc/stdio/getdelim.c b/lib/libc/stdio/getdelim.c index bb88633a12..48461577ee 100644 --- a/lib/libc/stdio/getdelim.c +++ b/lib/libc/stdio/getdelim.c @@ -120,8 +120,8 @@ getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim, goto error; } - if (*linecapp == 0) - *linep = NULL; + if (*linep == NULL) + *linecapp = 0; if (fp->pub._r <= 0 && __srefill(fp)) { /* If fp is at EOF already, we just need space for the NUL. */ -- 2.41.0