Commits

Anonymous committed a07d0b0

#32, Added sanity checks for allocations failures in gd_gd2

Comments (0)

Files changed (2)

      at very small dpi values (John Ellson/Graphviz)
 #31, Added DISABLE_THREADS to permit disabling of thread support
      (John Ellson/Graphviz)
+#32,  Added sanity checks for allocations failure in gd_gd2
       GD2_DBG (printf ("Reading %d chunk index entries\n", nc));
       sidx = sizeof (t_chunk_info) * nc;
       cidx = gdCalloc (sidx, 1);
+			if (!cidx) {
+				goto fail1;
+			}
       for (i = 0; i < nc; i++)
 	{
 	  if (gdGetInt (&cidx[i].offset, in) != 1)
   if (im == NULL)
     {
       return 0;
-    };
+    }
+
   bytesPerPixel = im->trueColor ? 4 : 1;
   nc = ncx * ncy;
 
       /* Allocate buffers */
       chunkMax = cs * bytesPerPixel * cs;
       chunkBuf = gdCalloc (chunkMax, 1);
+			if (!chunkBuf) {
+				goto fail2;
+			}
       compBuf = gdCalloc (compMax, 1);
+			if (!compBuf) {
+				goto fail2;
+			}
+
       GD2_DBG (printf ("Largest compressed chunk is %d bytes\n", compMax));
     };
 
 
 fail2:
   gdImageDestroy (im);
-  gdFree (chunkBuf);
-  gdFree (compBuf);
-  gdFree (chunkIdx);
+fail1:
+	if (chunkBuf) {
+		gdFree (chunkBuf);
+	}
+	if (compBuf) {
+		gdFree (compBuf);
+	} 
+	if (chunkIdx) {
+		gdFree (chunkIdx);
+	}
   return 0;
-
 }
 
 BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2Part (FILE * inFile, int srcx, int srcy, int w, int h)
 	  chunkMax = cs * cs;
 	}
       chunkBuf = gdCalloc (chunkMax, 1);
+			if (!chunkBuf) {
+				goto fail2;
+			}
       compBuf = gdCalloc (compMax, 1);
+			if (!compBuf) {
+				goto fail2;
+			}
+
     };
 
 /*      Don't bother with this... */
 fail2:
   gdImageDestroy (im);
 fail1:
-  gdFree (chunkBuf);
-  gdFree (compBuf);
-  gdFree (chunkIdx);
-
+	if (chunkBuf) {
+	  gdFree (chunkBuf);
+	}
+	if (compBuf) {
+	  gdFree (compBuf);
+	}
+	if (chunkIdx) {
+  	gdFree (chunkIdx);
+	}
   return 0;
 
 }
       /* Allocate the buffers.  */
       /* */
       chunkData = gdCalloc (cs * bytesPerPixel * cs, 1);
+			if (!chunkData) {
+				goto fail;
+			}
       compData = gdCalloc (compMax, 1);
+			if (!compData) {
+				goto fail;
+			}
 
       /* */
       /* Save the file position of chunk index, and allocate enough space for */
       gdSeek (out, posSave);
     };
 
+  /*printf("Memory block size is %d\n",gdTell(out)); */
+fail:
   GD2_DBG (printf ("Freeing memory\n"));
-  gdFree (chunkData);
-  gdFree (compData);
-  gdFree (chunkIdx);
-  GD2_DBG (printf ("Done\n"));
 
-  /*printf("Memory block size is %d\n",gdTell(out)); */
+	if (chunkData) {
+		gdFree (chunkData);
+	}
+	if (compData) {
+		gdFree (compData);
+	} 
+	if (chunkIdx) {
+		gdFree (chunkIdx);
+	}
+  GD2_DBG (printf ("Done\n"));
 
 }
 
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.