Blame SOURCES/gd-2.2.5-gdImageBmpPtr-double-free.patch

3220ab
From ac16bdf2d41724b5a65255d4c28fb0ec46bc42f5 Mon Sep 17 00:00:00 2001
3220ab
From: Mike Frysinger <vapier@gentoo.org>
3220ab
Date: Sat, 14 Jul 2018 13:54:08 -0400
3220ab
Subject: [PATCH] bmp: check return value in gdImageBmpPtr
3220ab
3220ab
Closes #447.
3220ab
---
3220ab
 src/gd_bmp.c | 17 ++++++++++++++---
3220ab
 1 file changed, 14 insertions(+), 3 deletions(-)
3220ab
3220ab
diff --git a/src/gd_bmp.c b/src/gd_bmp.c
3220ab
index bde0b9d3..78f40d9a 100644
3220ab
--- a/src/gd_bmp.c
3220ab
+++ b/src/gd_bmp.c
3220ab
@@ -47,6 +47,8 @@ static int bmp_read_4bit(gdImagePtr im, gdIOCtxPtr infile, bmp_info_t *info, bmp
3220ab
 static int bmp_read_8bit(gdImagePtr im, gdIOCtxPtr infile, bmp_info_t *info, bmp_hdr_t *header);
3220ab
 static int bmp_read_rle(gdImagePtr im, gdIOCtxPtr infile, bmp_info_t *info);
3220ab
 
3220ab
+static int _gdImageBmpCtx(gdImagePtr im, gdIOCtxPtr out, int compression);
3220ab
+
3220ab
 #define BMP_DEBUG(s)
3220ab
 
3220ab
 static int gdBMPPutWord(gdIOCtx *out, int w)
3220ab
@@ -87,8 +89,10 @@ BGD_DECLARE(void *) gdImageBmpPtr(gdImagePtr im, int *size, int compression)
3220ab
 	void *rv;
3220ab
 	gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
3220ab
 	if (out == NULL) return NULL;
3220ab
-	gdImageBmpCtx(im, out, compression);
3220ab
-	rv = gdDPExtractData(out, size);
3220ab
+	if (!_gdImageBmpCtx(im, out, compression))
3220ab
+		rv = gdDPExtractData(out, size);
3220ab
+	else
3220ab
+		rv = NULL;
3220ab
 	out->gd_free(out);
3220ab
 	return rv;
3220ab
 }
3220ab
@@ -141,6 +145,11 @@ BGD_DECLARE(void) gdImageBmp(gdImagePtr im, FILE *outFile, int compression)
3220ab
 		compression - whether to apply RLE or not.
3220ab
 */
3220ab
 BGD_DECLARE(void) gdImageBmpCtx(gdImagePtr im, gdIOCtxPtr out, int compression)
3220ab
+{
3220ab
+	_gdImageBmpCtx(im, out, compression);
3220ab
+}
3220ab
+
3220ab
+static int _gdImageBmpCtx(gdImagePtr im, gdIOCtxPtr out, int compression)
3220ab
 {
3220ab
 	int bitmap_size = 0, info_size, total_size, padding;
3220ab
 	int i, row, xpos, pixel;
3220ab
@@ -148,6 +157,7 @@ BGD_DECLARE(void) gdImageBmpCtx(gdImagePtr im, gdIOCtxPtr out, int compression)
3220ab
 	unsigned char *uncompressed_row = NULL, *uncompressed_row_start = NULL;
3220ab
 	FILE *tmpfile_for_compression = NULL;
3220ab
 	gdIOCtxPtr out_original = NULL;
3220ab
+	int ret = 1;
3220ab
 
3220ab
 	/* No compression if its true colour or we don't support seek */
3220ab
 	if (im->trueColor) {
3220ab
@@ -325,6 +335,7 @@ BGD_DECLARE(void) gdImageBmpCtx(gdImagePtr im, gdIOCtxPtr out, int compression)
3220ab
 		out_original = NULL;
3220ab
 	}
3220ab
 
3220ab
+	ret = 0;
3220ab
 cleanup:
3220ab
 	if (tmpfile_for_compression) {
3220ab
 #ifdef _WIN32
3220ab
@@ -338,7 +349,7 @@ BGD_DECLARE(void) gdImageBmpCtx(gdImagePtr im, gdIOCtxPtr out, int compression)
3220ab
 	if (out_original) {
3220ab
 		out_original->gd_free(out_original);
3220ab
 	}
3220ab
-	return;
3220ab
+	return ret;
3220ab
 }
3220ab
 
3220ab
 static int compress_row(unsigned char *row, int length)