Date: Wed, 3 Mar 2010 15:43:27 +0000 (UTC) From: Jaakko Heinonen <jh@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r204636 - head/lib/libc/stdlib Message-ID: <201003031543.o23FhRia092563@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: jh Date: Wed Mar 3 15:43:26 2010 New Revision: 204636 URL: http://svn.freebsd.org/changeset/base/204636 Log: In reallocf(3), free the memory only when size != 0. Otherwise, when the System V compatibility option (malloc "V" flag) is in effect a zero sized reallocf() could cause a double free. PR: bin/141753 Submitted by: Dan Lukes Modified: head/lib/libc/stdlib/reallocf.c Modified: head/lib/libc/stdlib/reallocf.c ============================================================================== --- head/lib/libc/stdlib/reallocf.c Wed Mar 3 15:05:58 2010 (r204635) +++ head/lib/libc/stdlib/reallocf.c Wed Mar 3 15:43:26 2010 (r204636) @@ -35,7 +35,14 @@ reallocf(void *ptr, size_t size) void *nptr; nptr = realloc(ptr, size); - if (!nptr && ptr) + + /* + * When the System V compatibility option (malloc "V" flag) is + * in effect, realloc(ptr, 0) frees the memory and returns NULL. + * So, to avoid double free, call free() only when size != 0. + * realloc(ptr, 0) can't fail when ptr != NULL. + */ + if (!nptr && ptr && size != 0) free(ptr); return (nptr); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201003031543.o23FhRia092563>