From 1a2d580a1262fb08d36e0c4be0e51d28be3c2eac Mon Sep 17 00:00:00 2001 From: giraffedata Date: Tue, 4 Sep 2007 02:13:39 +0000 Subject: make REALLOCARRAY free memory if it fails git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@400 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- lib/util/mallocvar.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'lib/util') diff --git a/lib/util/mallocvar.h b/lib/util/mallocvar.h index a26d007b..e5b7b1ea 100644 --- a/lib/util/mallocvar.h +++ b/lib/util/mallocvar.h @@ -57,11 +57,22 @@ static __inline__ void reallocProduct(void ** const blockP, unsigned int const factor1, unsigned int const factor2) { + + void * const oldBlockP = *blockP; + + void * newBlockP; if (UINT_MAX / factor2 < factor1) - *blockP = NULL; + newBlockP = NULL; else - *blockP = realloc(*blockP, factor1 * factor2); + newBlockP = realloc(oldBlockP, factor1 * factor2); + + if (newBlockP) + *blockP = newBlockP; + else { + free(oldBlockP); + *blockP = NULL; + } } -- cgit 1.4.1