Commits

Ondřej Surý  committed f0ddf2e

Introduce gdReallocEx which will free the original memory when realloc
fails to circumvent lost memory when doing:

a = gdRealloc(a, s);
if (!a)
return;

Thanks to Niels Thykier for catching that.

  • Participants
  • Parent commits 525ff74

Comments (0)

Files changed (5)

 		if (overflow2(sizeof (int), im->polyAllocated)) {
 			return;
 		}
-		im->polyInts = (int *) gdRealloc (im->polyInts,
-		                                  sizeof (int) * im->polyAllocated);
+		im->polyInts = (int *) gdReallocEx (im->polyInts,
+						    sizeof (int) * im->polyAllocated);
 		if (!im->polyInts) {
 			return;
 		}

File src/gd_topal.c

 	init_error_limit (oim, nim, cquantize);
 	arraysize = (size_t) ((nim->sx + 2) * (3 * sizeof (FSERROR)));
 	/* Allocate Floyd-Steinberg workspace. */
-	cquantize->fserrors = gdRealloc(cquantize->fserrors, arraysize);
+	cquantize->fserrors = gdReallocEx(cquantize->fserrors, arraysize);
 	if (!cquantize->fserrors) {
 		goto outOfMemory;
 	}
 				xshow_pos = 0;
 			} else if (xshow_pos + 20 > xshow_alloc) {
 				xshow_alloc += 100;
-				strex->xshow = gdRealloc(strex->xshow, xshow_alloc);
+				strex->xshow = gdReallocEx(strex->xshow, xshow_alloc);
 				if (!strex->xshow) {
 					if (tmpstr)
 						gdFree (tmpstr);
 		 * big enough for all paths to be tested.
 		 */
 		/* 2.0.22: Thorben Kundinger: +8 is needed, not +6. */
-		fullname = gdRealloc (fullname,
+		fullname = gdReallocEx(fullname,
 		                      strlen (fontsearchpath) + strlen (name) + 8);
 		if (!fullname) {
 			gdFree(fontlist);

File src/gdhelpers.c

 	return result;
 }
 
-void *
+inline void *
 gdCalloc (size_t nmemb, size_t size)
 {
 	return calloc (nmemb, size);
 }
 
-void *
+inline void *
 gdMalloc (size_t size)
 {
 	return malloc (size);
 }
 
-void *
+inline void *
 gdRealloc (void *ptr, size_t size)
 {
 	return realloc (ptr, size);
 }
 
+inline void *
+gdReallocEx (void *ptr, size_t size)
+{
+	void *newPtr = gdRealloc (ptr, size);
+	if (!newPtr && ptr)
+		gdFree(ptr);
+	return newPtr;
+}
+
 BGD_DECLARE(void) gdFree (void *ptr)
 {
 	free (ptr);

File src/gdhelpers.h

 	void *gdCalloc (size_t nmemb, size_t size);
 	void *gdMalloc (size_t size);
 	void *gdRealloc (void *ptr, size_t size);
+	/* The extended version of gdReallocEx will free *ptr if the
+	 * realloc fails */
+	void *gdReallocEx (void *ptr, size_t size);
 
 	/* Returns nonzero if multiplying the two quantities will
 		result in integer overflow. Also returns nonzero if