94b862
Backport of upstream commit:
94b862
94b862
From 411a4068f8c464e883358bf403a3e25158863823 Mon Sep 17 00:00:00 2001
94b862
From: Michael Adams <mdadams@ece.uvic.ca>
94b862
Date: Mon, 24 Oct 2016 06:56:08 -0700
94b862
Subject: [PATCH] Fixed a few bugs in the RAS encoder and decoder where errors
94b862
 were tested with assertions instead of being gracefully handled.
94b862
94b862
diff -pruN jasper-1.900.1.orig/src/libjasper/ras/ras_dec.c jasper-1.900.1/src/libjasper/ras/ras_dec.c
94b862
--- jasper-1.900.1.orig/src/libjasper/ras/ras_dec.c	2007-01-19 22:43:04.000000000 +0100
94b862
+++ jasper-1.900.1/src/libjasper/ras/ras_dec.c	2017-03-31 22:38:04.000000000 +0200
94b862
@@ -257,9 +257,16 @@ static int ras_getdatastd(jas_stream_t *
94b862
 	/* Avoid compiler warnings about unused parameters. */
94b862
 	cmap = 0;
94b862
 
94b862
+	assert(jas_image_numcmpts(image) <= 3);
94b862
+
94b862
+	for (i = 0; i < 3; ++i) {
94b862
+		data[i] = 0;
94b862
+	}
94b862
+
94b862
 	for (i = 0; i < jas_image_numcmpts(image); ++i) {
94b862
-		data[i] = jas_matrix_create(1, jas_image_width(image));
94b862
-		assert(data[i]);
94b862
+		if (!(data[i] = jas_matrix_create(1, jas_image_width(image)))) {
94b862
+			goto error;
94b862
+		}
94b862
 	}
94b862
 
94b862
 	pad = RAS_ROWSIZE(hdr) - (hdr->width * hdr->depth + 7) / 8;
94b862
@@ -270,7 +277,7 @@ static int ras_getdatastd(jas_stream_t *
94b862
 		for (x = 0; x < hdr->width; x++) {
94b862
 			while (nz < hdr->depth) {
94b862
 				if ((c = jas_stream_getc(in)) == EOF) {
94b862
-					return -1;
94b862
+					goto error;
94b862
 				}
94b862
 				z = (z << 8) | c;
94b862
 				nz += 8;
94b862
@@ -290,22 +297,31 @@ static int ras_getdatastd(jas_stream_t *
94b862
 		}
94b862
 		if (pad) {
94b862
 			if ((c = jas_stream_getc(in)) == EOF) {
94b862
-				return -1;
94b862
+				goto error;
94b862
 			}
94b862
 		}
94b862
 		for (i = 0; i < jas_image_numcmpts(image); ++i) {
94b862
 			if (jas_image_writecmpt(image, i, 0, y, hdr->width, 1,
94b862
 			  data[i])) {
94b862
-				return -1;
94b862
+				goto error;
94b862
 			}
94b862
 		}
94b862
 	}
94b862
 
94b862
 	for (i = 0; i < jas_image_numcmpts(image); ++i) {
94b862
 		jas_matrix_destroy(data[i]);
94b862
+		data[i] = 0;
94b862
 	}
94b862
 
94b862
 	return 0;
94b862
+
94b862
+error:
94b862
+	for (i = 0; i < 3; ++i) {
94b862
+		if (data[i]) {
94b862
+			jas_matrix_destroy(data[i]);
94b862
+		}
94b862
+	}
94b862
+	return -1;
94b862
 }
94b862
 
94b862
 static int ras_getcmap(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap)
94b862
@@ -324,7 +340,9 @@ static int ras_getcmap(jas_stream_t *in,
94b862
 		{
94b862
 		jas_eprintf("warning: palettized images not fully supported\n");
94b862
 		numcolors = 1 << hdr->depth;
94b862
-		assert(numcolors <= RAS_CMAP_MAXSIZ);
94b862
+		if (numcolors > RAS_CMAP_MAXSIZ) {
94b862
+			return -1;
94b862
+		}
94b862
 		actualnumcolors = hdr->maplength / 3;
94b862
 		for (i = 0; i < numcolors; i++) {
94b862
 			cmap->data[i] = 0;
94b862
diff -pruN jasper-1.900.1.orig/src/libjasper/ras/ras_enc.c jasper-1.900.1/src/libjasper/ras/ras_enc.c
94b862
--- jasper-1.900.1.orig/src/libjasper/ras/ras_enc.c	2017-03-31 22:20:38.000000000 +0200
94b862
+++ jasper-1.900.1/src/libjasper/ras/ras_enc.c	2017-03-31 22:38:04.000000000 +0200
94b862
@@ -230,9 +230,17 @@ static int ras_putdatastd(jas_stream_t *
94b862
 	jas_matrix_t *data[3];
94b862
 	int i;
94b862
 
94b862
+	assert(numcmpts <= 3);
94b862
+
94b862
+	for (i = 0; i < 3; ++i) {
94b862
+		data[i] = 0;
94b862
+	}
94b862
+
94b862
 	for (i = 0; i < numcmpts; ++i) {
94b862
-		data[i] = jas_matrix_create(jas_image_height(image), jas_image_width(image));
94b862
-		assert(data[i]);
94b862
+		if (!(data[i] = jas_matrix_create(jas_image_height(image),
94b862
+		  jas_image_width(image)))) {
94b862
+			goto error;
94b862
+		}
94b862
 	}
94b862
 
94b862
 	rowsize = RAS_ROWSIZE(hdr);
94b862
@@ -244,7 +252,7 @@ static int ras_putdatastd(jas_stream_t *
94b862
 		for (i = 0; i < numcmpts; ++i) {
94b862
 			if (jas_image_readcmpt(image, cmpts[i], 0, y,
94b862
 					jas_image_width(image), 1, data[i])) {
94b862
-				return -1;
94b862
+				goto error;
94b862
 			}
94b862
 		}
94b862
 		z = 0;
94b862
@@ -263,7 +271,7 @@ static int ras_putdatastd(jas_stream_t *
94b862
 			while (nz >= 8) {
94b862
 				c = (z >> (nz - 8)) & 0xff;
94b862
 				if (jas_stream_putc(out, c) == EOF) {
94b862
-					return -1;
94b862
+					goto error;
94b862
 				}
94b862
 				nz -= 8;
94b862
 				z &= RAS_ONES(nz);
94b862
@@ -272,21 +280,30 @@ static int ras_putdatastd(jas_stream_t *
94b862
 		if (nz > 0) {
94b862
 			c = (z >> (8 - nz)) & RAS_ONES(nz);
94b862
 			if (jas_stream_putc(out, c) == EOF) {
94b862
-				return -1;
94b862
+				goto error;
94b862
 			}
94b862
 		}
94b862
 		if (pad % 2) {
94b862
 			if (jas_stream_putc(out, 0) == EOF) {
94b862
-				return -1;
94b862
+				goto error;
94b862
 			}
94b862
 		}
94b862
 	}
94b862
 
94b862
 	for (i = 0; i < numcmpts; ++i) {
94b862
 		jas_matrix_destroy(data[i]);
94b862
+		data[i] = 0;
94b862
 	}
94b862
 
94b862
 	return 0;
94b862
+
94b862
+error:
94b862
+	for (i = 0; i < numcmpts; ++i) {
94b862
+		if (data[i]) {
94b862
+			jas_matrix_destroy(data[i]);
94b862
+		}
94b862
+	}
94b862
+	return -1;
94b862
 }
94b862
 
94b862
 static int ras_puthdr(jas_stream_t *out, ras_hdr_t *hdr)