Loading...
stdlib/FreeBSD/reallocf.c Libc-583 Libc-997.1.1
--- Libc/Libc-583/stdlib/FreeBSD/reallocf.c
+++ Libc/Libc-997.1.1/stdlib/FreeBSD/reallocf.c
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/lib/libc/stdlib/reallocf.c,v 1.4 2002/03/22 21:53:10 obrien Exp $");
+__FBSDID("$FreeBSD$");
 
 #include <stdlib.h>
 
@@ -35,7 +35,14 @@
 	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);
 }