Blame SOURCES/libtiff-CVE-2015-8781.patch

1ad933
diff --git a/libtiff/tif_luv.c b/libtiff/tif_luv.c
1ad933
index eba6c08..ebbd692 100644
1ad933
--- a/libtiff/tif_luv.c
1ad933
+++ b/libtiff/tif_luv.c
1ad933
@@ -202,7 +202,11 @@ LogL16Decode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
1ad933
 	if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
1ad933
 		tp = (int16*) op;
1ad933
 	else {
1ad933
-		assert(sp->tbuflen >= npixels);
1ad933
+		if(sp->tbuflen < npixels) {
1ad933
+            TIFFErrorExt(tif->tif_clientdata, module,
1ad933
+                    "Translation buffer too short");
1ad933
+            return (0);
1ad933
+        }
1ad933
 		tp = (int16*) sp->tbuf;
1ad933
 	}
1ad933
 	_TIFFmemset((void*) tp, 0, npixels*sizeof (tp[0]));
1ad933
@@ -211,9 +215,11 @@ LogL16Decode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
1ad933
 	cc = tif->tif_rawcc;
1ad933
 	/* get each byte string */
1ad933
 	for (shft = 2*8; (shft -= 8) >= 0; ) {
1ad933
-		for (i = 0; i < npixels && cc > 0; )
1ad933
+		for (i = 0; i < npixels && cc > 0; ) {
1ad933
 			if (*bp >= 128) {		/* run */
1ad933
-				rc = *bp++ + (2-128);   /* TODO: potential input buffer overrun when decoding corrupt or truncated data */
1ad933
+                if ( cc < 2 )
1ad933
+                    break;
1ad933
+				rc = *bp++ + (2-128);
1ad933
 				b = (int16)(*bp++ << shft);
1ad933
 				cc -= 2;
1ad933
 				while (rc-- && i < npixels)
1ad933
@@ -223,6 +229,7 @@ LogL16Decode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
1ad933
 				while (--cc && rc-- && i < npixels)
1ad933
 					tp[i++] |= (int16)*bp++ << shft;
1ad933
 			}
1ad933
+        }
1ad933
 		if (i != npixels) {
1ad933
 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
1ad933
 			TIFFErrorExt(tif->tif_clientdata, module,
1ad933
@@ -268,13 +275,17 @@ LogLuvDecode24(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
1ad933
 	if (sp->user_datafmt == SGILOGDATAFMT_RAW)
1ad933
 		tp = (uint32 *)op;
1ad933
 	else {
1ad933
-		assert(sp->tbuflen >= npixels);
1ad933
+		if(sp->tbuflen < npixels) {
1ad933
+            TIFFErrorExt(tif->tif_clientdata, module,
1ad933
+                    "Translation buffer too short");
1ad933
+            return (0);
1ad933
+        }
1ad933
 		tp = (uint32 *) sp->tbuf;
1ad933
 	}
1ad933
 	/* copy to array of uint32 */
1ad933
 	bp = (unsigned char*) tif->tif_rawcp;
1ad933
 	cc = tif->tif_rawcc;
1ad933
-	for (i = 0; i < npixels && cc > 0; i++) {
1ad933
+	for (i = 0; i < npixels && cc >= 3; i++) {
1ad933
 		tp[i] = bp[0] << 16 | bp[1] << 8 | bp[2];
1ad933
 		bp += 3;
1ad933
 		cc -= 3;
1ad933
@@ -325,7 +336,11 @@ LogLuvDecode32(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
1ad933
 	if (sp->user_datafmt == SGILOGDATAFMT_RAW)
1ad933
 		tp = (uint32*) op;
1ad933
 	else {
1ad933
-		assert(sp->tbuflen >= npixels);
1ad933
+		if(sp->tbuflen < npixels) {
1ad933
+            TIFFErrorExt(tif->tif_clientdata, module,
1ad933
+                    "Translation buffer too short");
1ad933
+            return (0);
1ad933
+        }
1ad933
 		tp = (uint32*) sp->tbuf;
1ad933
 	}
1ad933
 	_TIFFmemset((void*) tp, 0, npixels*sizeof (tp[0]));
1ad933
@@ -334,11 +349,13 @@ LogLuvDecode32(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
1ad933
 	cc = tif->tif_rawcc;
1ad933
 	/* get each byte string */
1ad933
 	for (shft = 4*8; (shft -= 8) >= 0; ) {
1ad933
-		for (i = 0; i < npixels && cc > 0; )
1ad933
+		for (i = 0; i < npixels && cc > 0; ) {
1ad933
 			if (*bp >= 128) {		/* run */
1ad933
+                if ( cc < 2 )
1ad933
+                    break;
1ad933
 				rc = *bp++ + (2-128);
1ad933
 				b = (uint32)*bp++ << shft;
1ad933
-				cc -= 2;                /* TODO: potential input buffer overrun when decoding corrupt or truncated data */
1ad933
+				cc -= 2;
1ad933
 				while (rc-- && i < npixels)
1ad933
 					tp[i++] |= b;
1ad933
 			} else {			/* non-run */
1ad933
@@ -346,6 +363,7 @@ LogLuvDecode32(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
1ad933
 				while (--cc && rc-- && i < npixels)
1ad933
 					tp[i++] |= (uint32)*bp++ << shft;
1ad933
 			}
1ad933
+        }
1ad933
 		if (i != npixels) {
1ad933
 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
1ad933
 			TIFFErrorExt(tif->tif_clientdata, module,
1ad933
@@ -407,6 +425,7 @@ LogLuvDecodeTile(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
1ad933
 static int
1ad933
 LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
1ad933
 {
1ad933
+    static const char module [] = "LogL16Encode";
1ad933
 	LogLuvState* sp = EncoderState(tif);
1ad933
 	int shft;
1ad933
 	tmsize_t i;
1ad933
@@ -427,7 +446,11 @@ LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
1ad933
 		tp = (int16*) bp;
1ad933
 	else {
1ad933
 		tp = (int16*) sp->tbuf;
1ad933
-		assert(sp->tbuflen >= npixels);
1ad933
+		if(sp->tbuflen < npixels) {
1ad933
+            TIFFErrorExt(tif->tif_clientdata, module,
1ad933
+                    "Translation buffer too short");
1ad933
+            return (0);
1ad933
+        }
1ad933
 		(*sp->tfunc)(sp, bp, npixels);
1ad933
 	}
1ad933
 	/* compress each byte string */
1ad933
@@ -500,6 +523,7 @@ LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
1ad933
 static int
1ad933
 LogLuvEncode24(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
1ad933
 {
1ad933
+    static const char module [] = "LogLuvEncode24";
1ad933
 	LogLuvState* sp = EncoderState(tif);
1ad933
 	tmsize_t i;
1ad933
 	tmsize_t npixels;
1ad933
@@ -515,7 +539,11 @@ LogLuvEncode24(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
1ad933
 		tp = (uint32*) bp;
1ad933
 	else {
1ad933
 		tp = (uint32*) sp->tbuf;
1ad933
-		assert(sp->tbuflen >= npixels);
1ad933
+		if(sp->tbuflen < npixels) {
1ad933
+            TIFFErrorExt(tif->tif_clientdata, module,
1ad933
+                    "Translation buffer too short");
1ad933
+            return (0);
1ad933
+        }
1ad933
 		(*sp->tfunc)(sp, bp, npixels);
1ad933
 	}
1ad933
 	/* write out encoded pixels */
1ad933
@@ -547,6 +575,7 @@ LogLuvEncode24(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
1ad933
 static int
1ad933
 LogLuvEncode32(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
1ad933
 {
1ad933
+    static const char module [] = "LogLuvEncode32";
1ad933
 	LogLuvState* sp = EncoderState(tif);
1ad933
 	int shft;
1ad933
 	tmsize_t i;
1ad933
@@ -568,7 +597,11 @@ LogLuvEncode32(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
1ad933
 		tp = (uint32*) bp;
1ad933
 	else {
1ad933
 		tp = (uint32*) sp->tbuf;
1ad933
-		assert(sp->tbuflen >= npixels);
1ad933
+		if(sp->tbuflen < npixels) {
1ad933
+            TIFFErrorExt(tif->tif_clientdata, module,
1ad933
+                    "Translation buffer too short");
1ad933
+            return (0);
1ad933
+        }
1ad933
 		(*sp->tfunc)(sp, bp, npixels);
1ad933
 	}
1ad933
 	/* compress each byte string */