Ondřej Surý avatar Ondřej Surý committed 1a3f355

Fix some unchecked values and possibly dereferenced null pointers in tests/

Comments (0)

Files changed (5)

tests/gdimagefill/bug00002_1.c

 #include <stdio.h>
 #include "gdtest.h"
 
+#define TMP_FN "bug00002_1.png"
+
 int main()
 {
 	gdImagePtr im;
-	int error = 0;
 	FILE *fp;
 	char path[1024];
 
-/*	fputs("flag 0\n", stdout); */
 	im = gdImageCreateTrueColor(100, 100);
 
-/*	fputs("flag 1\n", stdout); */
+	if (im == NULL) {
+		gdTestErrorMsg("Cannot create image.\n");
+		return 1;
+	}
+
 	gdImageFill(im, 0, 0, 0xffffff);
-/*	fputs("flag 2\n", stdout); */
 	gdImageFill(im, 0, 0, 0xffffff);
-/*	fputs("flag 3\n", stdout); */
 
-	fp = fopen("a.png", "wb");
-	/* Write img to stdout */
+	fp = fopen(TMP_FN, "wb");
+	if (fp == NULL) {
+		gdTestErrorMsg("Cannot create image from <%s>\n", TMP_FN);
+		gdImageDestroy(im);
+		return 1;
+	}
+
 	gdImagePng(im,fp);
 	fclose(fp);
 
 	sprintf(path, "%s/gdimagefill/bug00002_1_exp.png", GDTEST_TOP_DIR);
 	if (!gdAssertImageEqualsToFile(path, im)) {
-		error = 1;
+		gdImageDestroy(im);
+		return 1;
 	}
 
-	/* Destroy it */
 	gdImageDestroy(im);
-	return error;
+
+	if (remove(TMP_FN) == -1) {
+		gdTestErrorMsg("Cannot remove temporary file: <%s>\n", TMP_FN);
+		return 1;
+	}
+
+	return 0;
 }
 

tests/gdtiled/bug00032.c

 int main()
 {
 	gdImagePtr im, tile;
-	char path[1024];
+	char path[GDTEST_STRING_MAX];
 
 	gdSetErrorMethod(gdSilence);
 
-	snprintf(path, 1023, "%s/gdtiled/%s", GDTEST_TOP_DIR, exp_img);
+	snprintf(path, GDTEST_STRING_MAX - 1, "%s/gdtiled/%s", GDTEST_TOP_DIR, exp_img);
 
 	tile = gdImageCreateTrueColor(10, 10);
 	gdImageFill(tile, 0, 0, 0xFFFFFF);
 	gdImageSetTile(im, tile);
 	gdImageFilledRectangle(im, 10, 10, 49, 49, gdTiled);
 
-	gdAssertImageEqualsToFile(path, im);
+	if (!gdAssertImageEqualsToFile(path, im)) {
+		gdImageDestroy(im);
+		gdImageDestroy(tile);
+		return 1;
+	}
 
-	/* Destroy it */
 	gdImageDestroy(im);
 	gdImageDestroy(tile);
 	return 0;

tests/gif/bug00006.c

 #include "gd.h"
 #include "gdtest.h"
 
+#define TMP_FN "_tmp_bug0006.gif"
+
 int main()
 {
 	gdImagePtr im;
 	int error = 0;
 
 	im = gdImageCreateTrueColor(192, 36);
+	if (im == NULL) {
+		gdTestErrorMsg("Cannot create image\n");
+		return 1;
+	}
+
 	gdImageColorTransparent(im, trans_c);
 	gdImageFilledRectangle(im, 0,0, 192,36, trans_c);
 
-	fp = fopen("_tmp_bug0006.gif", "wb");
+	fp = fopen(TMP_FN, "wb");
+	if (!fp) {
+		gdTestErrorMsg("Cannot open <%s> for writing\n", TMP_FN);
+		return 1;
+	}
 	gdImageGif(im,fp);
 	fclose(fp);
 
 	gdImageDestroy(im);
 
-	fp = fopen("_tmp_bug0006.gif", "rb");
+	fp = fopen(TMP_FN, "rb");
 	if (!fp) {
-		gdTestErrorMsg("Cannot open <%s>\n", "_tmp_bug0006.gif");
+		gdTestErrorMsg("Cannot open <%s> for reading\n", TMP_FN);
 		return 1;
 	}
-
 	im = gdImageCreateFromGif(fp);
 	fclose(fp);
+
 	if (!im) {
-		gdTestErrorMsg("Cannot create image from <%s>\n", "_tmp_bug0006.gif");
+		gdTestErrorMsg("Cannot create image from <%s>\n", TMP_FN);
+		return 1;
 	}
 
-	trans_c_f =gdImageGetTransparent(im);
+	trans_c_f = gdImageGetTransparent(im);
 	if (gdTestAssert(trans_c_f == 1)) {
 		r_f = gdImageRed(im, trans_c_f);
 		g_f = gdImageGreen(im, trans_c_f);
 
 	/* Destroy it */
 	gdImageDestroy(im);
-	remove("_tmp_bug0006.gif");
+	if (remove(TMP_FN) == -1) {
+		gdTestErrorMsg("Cannot remove file: <%s>\n", TMP_FN);
+	}
 	return error;
 }

tests/gif/bug00181.c

 
 	/* GIFEncode */
 	im = gdImageCreate(100, 100);
+        if (!im) {
+                gdTestErrorMsg("Cannot create image.\n");
+                return 1;
+        }
 	im->interlace = 1;
 	fp = fopen("bug00181.gif", "wb");
+	if (!fp) {
+		gdTestErrorMsg("Cannot open <%s> for writing.\n", "bug00181.gif");
+		return 1;
+	}
 	gdImageGif(im, fp);
 	gdImageDestroy(im);
 	fclose(fp);
 
 	fp = fopen("bug00181.gif", "rb");
+	if (!fp) {
+		gdTestErrorMsg("Cannot open <%s> for reading.\n", "bug00181.gif");
+		return 1;
+	}
 	im = gdImageCreateFromGif(fp);
+	fclose(fp);
+        if (!im) {
+                gdTestErrorMsg("Cannot create image from <%s>\n", "bug00181.gif");
+                return 1;
+        }
 	error = !im->interlace;
 	gdImageDestroy(im);
-	fclose(fp);
 
 	if (error) return error;
 
 	trans = gdImageColorAllocate(im, 1, 1, 1);
 	gdImageRectangle(im, 0, 0, 10, 10, black);
 	fp = fopen("bug00181a.gif", "wb");
+	if (!fp) {
+		gdTestErrorMsg("Cannot open <%s> for writing.\n", "bug00181a.gif");
+		return 1;
+	}
 	gdImageGifAnimBegin(im, fp, 1, 3);
 	gdImageGifAnimAdd(im, fp, 0, 0, 0, 100, 1, NULL);
 	im2 = gdImageCreate(100, 100);
 	gdImageDestroy(im3);
 
 	fp = fopen("bug00181a.gif", "rb");
+	if (!fp) {
+		gdTestErrorMsg("Cannot open <%s> for reading.\n", "bug00181a.gif");
+		return 1;
+	}
 	im = gdImageCreateFromGif(fp);
+	fclose(fp);
+        if (!im) {
+                gdTestErrorMsg("Cannot create image from <%s>\n", "bug00181a.gif");
+                return 1;
+        }
 	error = !im->interlace;
 	gdImageDestroy(im);
-	fclose(fp);
 
 	return error;
 }

tests/png/png_resolution.c

 	gdImageDestroy(im);
 
 	im = gdImageCreateFromPngPtr(size, data);
-	gdTestAssert(gdImageResolutionX(im) == 72);
-	gdTestAssert(gdImageResolutionY(im) == 300);
+	if (gdTestAssert(gdImageResolutionX(im) == 72) || gdTestAssert(gdImageResolutionY(im) == 300)) {
+		gdFree(data);
+		gdImageDestroy(im);
+		return 1;
+	}
+		
 	gdFree(data);
 	gdImageDestroy(im);
 	return 0;
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.