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

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