Ondřej Surý avatar 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.

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;
 		}
 	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);
 	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);
 	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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.