83be9e
From a632c6b54bd4ffc3bebab420e00b7e7688aa3846 Mon Sep 17 00:00:00 2001
83be9e
From: Michael Adams <mdadams@ece.uvic.ca>
83be9e
Date: Fri, 30 Dec 2016 07:27:48 -0800
83be9e
Subject: [PATCH] Fixed a problem in the JP2 encoder that caused a null pointer
83be9e
 dereference when no ICC profile data is available (e.g., in the case of an
83be9e
 unknown color space). Reference:    
83be9e
 https://github.com/mdadams/jasper/issues/109
83be9e
83be9e
---
83be9e
 src/libjasper/jp2/jp2_enc.c | 46 +++++++++++++++++++++++++++++++++------------
83be9e
 1 file changed, 34 insertions(+), 12 deletions(-)
83be9e
83be9e
diff --git a/src/libjasper/jp2/jp2_enc.c b/src/libjasper/jp2/jp2_enc.c
83be9e
index bca3ca6..b979216 100644
83be9e
--- a/src/libjasper/jp2/jp2_enc.c
83be9e
+++ b/src/libjasper/jp2/jp2_enc.c
83be9e
@@ -112,6 +112,8 @@ int jp2_encode(jas_image_t *image, jas_stream_t *out, const char *optstr)
83be9e
 
83be9e
 	box = 0;
83be9e
 	tmpstream = 0;
83be9e
+	iccstream = 0;
83be9e
+	iccprof = 0;
83be9e
 
83be9e
 	allcmptssame = 1;
83be9e
 	sgnd = jas_image_cmptsgnd(image, 0);
83be9e
@@ -225,22 +227,36 @@ int jp2_encode(jas_image_t *image, jas_stream_t *out, const char *optstr)
83be9e
 		colr->method = JP2_COLR_ICC;
83be9e
 		colr->pri = JP2_COLR_PRI;
83be9e
 		colr->approx = 0;
83be9e
-		iccprof = jas_iccprof_createfromcmprof(jas_image_cmprof(image));
83be9e
-		assert(iccprof);
83be9e
-		iccstream = jas_stream_memopen(0, 0);
83be9e
-		assert(iccstream);
83be9e
-		if (jas_iccprof_save(iccprof, iccstream))
83be9e
-			abort();
83be9e
-		if ((pos = jas_stream_tell(iccstream)) < 0)
83be9e
-			abort();
83be9e
+		/* Ensure that cmprof_ is not null. */
83be9e
+		if (!jas_image_cmprof(image)) {
83be9e
+			goto error;
83be9e
+		}
83be9e
+		if (!(iccprof = jas_iccprof_createfromcmprof(
83be9e
+		  jas_image_cmprof(image)))) {
83be9e
+			goto error;
83be9e
+		}
83be9e
+		if (!(iccstream = jas_stream_memopen(0, 0))) {
83be9e
+			goto error;
83be9e
+		}
83be9e
+		if (jas_iccprof_save(iccprof, iccstream)) {
83be9e
+			goto error;
83be9e
+		}
83be9e
+		if ((pos = jas_stream_tell(iccstream)) < 0) {
83be9e
+			goto error;
83be9e
+		}
83be9e
 		colr->iccplen = pos;
83be9e
-		colr->iccp = jas_malloc(pos);
83be9e
-		assert(colr->iccp);
83be9e
+		if (!(colr->iccp = jas_malloc(pos))) {
83be9e
+			goto error;
83be9e
+		}
83be9e
 		jas_stream_rewind(iccstream);
83be9e
-		if (jas_stream_read(iccstream, colr->iccp, colr->iccplen) != colr->iccplen)
83be9e
-			abort();
83be9e
+		if (jas_stream_read(iccstream, colr->iccp, colr->iccplen) !=
83be9e
+		  colr->iccplen) {
83be9e
+			goto error;
83be9e
+		}
83be9e
 		jas_stream_close(iccstream);
83be9e
+		iccstream = 0;
83be9e
 		jas_iccprof_destroy(iccprof);
83be9e
+		iccprof = 0;
83be9e
 		break;
83be9e
 	}
83be9e
 	if (jp2_box_put(box, tmpstream)) {
83be9e
@@ -354,6 +370,12 @@ int jp2_encode(jas_image_t *image, jas_stream_t *out, const char *optstr)
83be9e
 
83be9e
 error:
83be9e
 
83be9e
+	if (iccprof) {
83be9e
+		jas_iccprof_destroy(iccprof);
83be9e
+	}
83be9e
+	if (iccstream) {
83be9e
+		jas_stream_close(iccstream);
83be9e
+	}
83be9e
 	if (box) {
83be9e
 		jp2_box_destroy(box);
83be9e
 	}