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

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