8ca2e9
From 7704f4b45c7808a6ea73d4b6684f36124ba37c11 Mon Sep 17 00:00:00 2001
8ca2e9
From: =?UTF-8?q?Nikola=20Forr=C3=B3?= <nforro@redhat.com>
8ca2e9
Date: Wed, 12 Jun 2019 13:57:49 +0200
8ca2e9
Subject: [PATCH] Fix important Covscan defects
8ca2e9
8ca2e9
---
8ca2e9
 contrib/addtiffo/tif_ovrcache.c |  1 +
8ca2e9
 contrib/iptcutil/iptcutil.c     |  4 +++-
8ca2e9
 libtiff/tif_ojpeg.c             | 10 ++++++++++
8ca2e9
 libtiff/tif_open.c              |  1 +
8ca2e9
 test/ascii_tag.c                |  2 +-
8ca2e9
 test/long_tag.c                 |  2 +-
8ca2e9
 test/short_tag.c                |  2 +-
8ca2e9
 test/strip.c                    |  2 +-
8ca2e9
 tools/tiff2pdf.c                |  2 ++
8ca2e9
 tools/tiffcp.c                  |  6 +++++-
8ca2e9
 tools/tiffcrop.c                |  1 +
8ca2e9
 tools/tiffdither.c              |  3 ++-
8ca2e9
 tools/tiffsplit.c               |  2 ++
8ca2e9
 13 files changed, 31 insertions(+), 7 deletions(-)
8ca2e9
8ca2e9
diff --git a/contrib/addtiffo/tif_ovrcache.c b/contrib/addtiffo/tif_ovrcache.c
8ca2e9
index 646b534..1d183ab 100644
8ca2e9
--- a/contrib/addtiffo/tif_ovrcache.c
8ca2e9
+++ b/contrib/addtiffo/tif_ovrcache.c
8ca2e9
@@ -110,6 +110,7 @@ TIFFOvrCache *TIFFCreateOvrCache( TIFF *hTIFF, int nDirOffset )
8ca2e9
 		TIFFErrorExt( hTIFF->tif_clientdata, hTIFF->tif_name,
8ca2e9
 					  "Can't allocate memory for overview cache." );
8ca2e9
         /* TODO: use of TIFFError is inconsistent with use of fprintf in addtiffo.c, sort out */
8ca2e9
+        _TIFFfree( psCache );
8ca2e9
         return NULL;
8ca2e9
     }
8ca2e9
 
8ca2e9
diff --git a/contrib/iptcutil/iptcutil.c b/contrib/iptcutil/iptcutil.c
8ca2e9
index 557a67e..b6be247 100644
8ca2e9
--- a/contrib/iptcutil/iptcutil.c
8ca2e9
+++ b/contrib/iptcutil/iptcutil.c
8ca2e9
@@ -293,8 +293,10 @@ int formatIPTC(FILE *ifile, FILE *ofile)
8ca2e9
     for (tagindx=0; tagindx
8ca2e9
     {
8ca2e9
       c = str[tagindx] = getc(ifile);
8ca2e9
-      if (c == EOF)
8ca2e9
+      if (c == EOF) {
8ca2e9
+        free(str);
8ca2e9
         return -1;
8ca2e9
+      }
8ca2e9
     }
8ca2e9
     str[ taglen ] = 0;
8ca2e9
 
8ca2e9
diff --git a/libtiff/tif_ojpeg.c b/libtiff/tif_ojpeg.c
8ca2e9
index 336d47d..3005dcd 100644
8ca2e9
--- a/libtiff/tif_ojpeg.c
8ca2e9
+++ b/libtiff/tif_ojpeg.c
8ca2e9
@@ -1392,11 +1392,15 @@ OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif)
8ca2e9
 			nb[sizeof(uint32)+2]=0;
8ca2e9
 			nb[sizeof(uint32)+3]=67;
8ca2e9
 			if (OJPEGReadBlock(sp,65,&nb[sizeof(uint32)+4])==0)
8ca2e9
+			{
8ca2e9
+				_TIFFfree(nb);
8ca2e9
 				return(0);
8ca2e9
+			}
8ca2e9
 			o=nb[sizeof(uint32)+4]&1;;
8ca2e9
 			if (3
8ca2e9
 			{
8ca2e9
 				TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
8ca2e9
+				_TIFFfree(nb);
8ca2e9
 				return(0);
8ca2e9
 			}
8ca2e9
 			if (sp->qtable[o]!=0)
8ca2e9
@@ -1446,13 +1450,17 @@ OJPEGReadHeaderInfoSecStreamDht(TIFF* tif)
8ca2e9
 		nb[sizeof(uint32)+2]=(m>>8);
8ca2e9
 		nb[sizeof(uint32)+3]=(m&255);
8ca2e9
 		if (OJPEGReadBlock(sp,m-2,&nb[sizeof(uint32)+4])==0)
8ca2e9
+		{
8ca2e9
+			_TIFFfree(nb);
8ca2e9
 			return(0);
8ca2e9
+		}
8ca2e9
 		o=nb[sizeof(uint32)+4];
8ca2e9
 		if ((o&240)==0)
8ca2e9
 		{
8ca2e9
 			if (3
8ca2e9
 			{
8ca2e9
 				TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
8ca2e9
+				_TIFFfree(nb);
8ca2e9
 				return(0);
8ca2e9
 			}
8ca2e9
 			if (sp->dctable[o]!=0)
8ca2e9
@@ -1464,12 +1472,14 @@ OJPEGReadHeaderInfoSecStreamDht(TIFF* tif)
8ca2e9
 			if ((o&240)!=16)
8ca2e9
 			{
8ca2e9
 				TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
8ca2e9
+				_TIFFfree(nb);
8ca2e9
 				return(0);
8ca2e9
 			}
8ca2e9
 			o&=15;
8ca2e9
 			if (3
8ca2e9
 			{
8ca2e9
 				TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
8ca2e9
+				_TIFFfree(nb);
8ca2e9
 				return(0);
8ca2e9
 			}
8ca2e9
 			if (sp->actable[o]!=0)
8ca2e9
diff --git a/libtiff/tif_open.c b/libtiff/tif_open.c
8ca2e9
index 3b3b2ce..7578275 100644
8ca2e9
--- a/libtiff/tif_open.c
8ca2e9
+++ b/libtiff/tif_open.c
8ca2e9
@@ -175,6 +175,7 @@ TIFFClientOpen(
8ca2e9
 	if (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc) {
8ca2e9
 		TIFFErrorExt(clientdata, module,
8ca2e9
 			  "One of the client procedures is NULL pointer.");
8ca2e9
+		_TIFFfree(tif);
8ca2e9
 		goto bad2;
8ca2e9
 	}
8ca2e9
 	tif->tif_readproc = readproc;
8ca2e9
diff --git a/test/ascii_tag.c b/test/ascii_tag.c
8ca2e9
index bf81212..0e85c8f 100644
8ca2e9
--- a/test/ascii_tag.c
8ca2e9
+++ b/test/ascii_tag.c
8ca2e9
@@ -125,7 +125,7 @@ main(int argc, char **argv)
8ca2e9
 	}
8ca2e9
 
8ca2e9
 	/* Write dummy pixel data. */
8ca2e9
-	if (!TIFFWriteScanline(tif, buf, 0, 0) < 0) {
8ca2e9
+	if (TIFFWriteScanline(tif, buf, 0, 0) == -1) {
8ca2e9
 		fprintf (stderr, "Can't write image data.\n");
8ca2e9
 		goto failure;
8ca2e9
 	}
8ca2e9
diff --git a/test/long_tag.c b/test/long_tag.c
8ca2e9
index 256bc8e..e895ee4 100644
8ca2e9
--- a/test/long_tag.c
8ca2e9
+++ b/test/long_tag.c
8ca2e9
@@ -109,7 +109,7 @@ main(int argc, char **argv)
8ca2e9
 	}
8ca2e9
 
8ca2e9
 	/* Write dummy pixel data. */
8ca2e9
-	if (!TIFFWriteScanline(tif, buf, 0, 0) < 0) {
8ca2e9
+	if (TIFFWriteScanline(tif, buf, 0, 0) == -1) {
8ca2e9
 		fprintf (stderr, "Can't write image data.\n");
8ca2e9
 		goto failure;
8ca2e9
 	}
8ca2e9
diff --git a/test/short_tag.c b/test/short_tag.c
8ca2e9
index 45214e1..c9e0c21 100644
8ca2e9
--- a/test/short_tag.c
8ca2e9
+++ b/test/short_tag.c
8ca2e9
@@ -123,7 +123,7 @@ main(int argc, char **argv)
8ca2e9
 	}
8ca2e9
 
8ca2e9
 	/* Write dummy pixel data. */
8ca2e9
-	if (!TIFFWriteScanline(tif, buf, 0, 0) < 0) {
8ca2e9
+	if (TIFFWriteScanline(tif, buf, 0, 0) == -1) {
8ca2e9
 		fprintf (stderr, "Can't write image data.\n");
8ca2e9
 		goto failure;
8ca2e9
 	}
8ca2e9
diff --git a/test/strip.c b/test/strip.c
8ca2e9
index df6406e..ab7f5ef 100644
8ca2e9
--- a/test/strip.c
8ca2e9
+++ b/test/strip.c
8ca2e9
@@ -278,7 +278,7 @@ write_scanlines(TIFF *tif, const tdata_t array, const tsize_t size)
8ca2e9
 	}
8ca2e9
 
8ca2e9
 	for (offset = 0, row = 0; row < length; offset+=scanlinesize, row++) {
8ca2e9
-		if (TIFFWriteScanline(tif, (char *)array + offset, row, 0) < 0) {
8ca2e9
+		if (TIFFWriteScanline(tif, (char *)array + offset, row, 0) == -1) {
8ca2e9
 			fprintf (stderr,
8ca2e9
 				 "Can't write image data at row %u.\n", row);
8ca2e9
 			return -1;
8ca2e9
diff --git a/tools/tiff2pdf.c b/tools/tiff2pdf.c
8ca2e9
index ac5d70d..a4ce325 100644
8ca2e9
--- a/tools/tiff2pdf.c
8ca2e9
+++ b/tools/tiff2pdf.c
8ca2e9
@@ -2440,6 +2440,7 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){
8ca2e9
 					t2p->tiff_datasize, 
8ca2e9
 					TIFFFileName(input));
8ca2e9
 				t2p->t2p_error = T2P_ERR_ERROR;
8ca2e9
+				_TIFFfree(buffer);
8ca2e9
 				return(0);
8ca2e9
 			}
8ca2e9
 			for(i=0;i
8ca2e9
@@ -2919,6 +2920,7 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_
8ca2e9
 					t2p->tiff_datasize, 
8ca2e9
 					TIFFFileName(input));
8ca2e9
 				t2p->t2p_error = T2P_ERR_ERROR;
8ca2e9
+				_TIFFfree(buffer);
8ca2e9
 				return(0);
8ca2e9
 			}
8ca2e9
 			samplebufferoffset=0;
8ca2e9
diff --git a/tools/tiffcp.c b/tools/tiffcp.c
8ca2e9
index 48319fa..a54e65d 100644
8ca2e9
--- a/tools/tiffcp.c
8ca2e9
+++ b/tools/tiffcp.c
8ca2e9
@@ -1191,10 +1191,14 @@ DECLAREreadFunc(readSeparateStripsIntoBuffer)
8ca2e9
 {
8ca2e9
 	int status = 1;
8ca2e9
 	tsize_t scanlinesize = TIFFScanlineSize(in);
8ca2e9
-	tdata_t scanline = _TIFFmalloc(scanlinesize);
8ca2e9
+	tdata_t scanline;
8ca2e9
 	if (!scanlinesize)
8ca2e9
 		return 0;
8ca2e9
 
8ca2e9
+	scanline = _TIFFmalloc(scanlinesize);
8ca2e9
+	if (!scanline)
8ca2e9
+		return 0;
8ca2e9
+
8ca2e9
 	(void) imagewidth;
8ca2e9
 	if (scanline) {
8ca2e9
 		uint8* bufp = (uint8*) buf;
8ca2e9
diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
8ca2e9
index 7684318..a5d0231 100644
8ca2e9
--- a/tools/tiffcrop.c
8ca2e9
+++ b/tools/tiffcrop.c
8ca2e9
@@ -2576,6 +2576,7 @@ static void dump_info(FILE *dumpfile, int format, char *prefix, char *msg, ...)
8ca2e9
     fprintf(dumpfile, "%s ", prefix);
8ca2e9
     vfprintf(dumpfile, msg, ap);
8ca2e9
     fprintf(dumpfile, "\n");
8ca2e9
+    va_end(ap);
8ca2e9
     }
8ca2e9
   }
8ca2e9
 
8ca2e9
diff --git a/tools/tiffdither.c b/tools/tiffdither.c
8ca2e9
index 86160f2..5ceb314 100644
8ca2e9
--- a/tools/tiffdither.c
8ca2e9
+++ b/tools/tiffdither.c
8ca2e9
@@ -77,7 +77,7 @@ fsdither(TIFF* in, TIFF* out)
8ca2e9
 	 * Get first line
8ca2e9
 	 */
8ca2e9
 	if (TIFFReadScanline(in, inputline, 0, 0) <= 0)
8ca2e9
-		return;
8ca2e9
+		goto skip_on_error;
8ca2e9
 	inptr = inputline;
8ca2e9
 	nextptr = nextline;
8ca2e9
 	for (j = 0; j < imagewidth; ++j)
8ca2e9
@@ -128,6 +128,7 @@ fsdither(TIFF* in, TIFF* out)
8ca2e9
 		if (TIFFWriteScanline(out, outline, i-1, 0) < 0)
8ca2e9
 			break;
8ca2e9
 	}
8ca2e9
+skip_on_error:
8ca2e9
 	_TIFFfree(inputline);
8ca2e9
 	_TIFFfree(thisline);
8ca2e9
 	_TIFFfree(nextline);
8ca2e9
diff --git a/tools/tiffsplit.c b/tools/tiffsplit.c
8ca2e9
index 135de2e..03b5558 100644
8ca2e9
--- a/tools/tiffsplit.c
8ca2e9
+++ b/tools/tiffsplit.c
8ca2e9
@@ -239,6 +239,7 @@ cpStrips(TIFF* in, TIFF* out)
8ca2e9
 
8ca2e9
 		if (!TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts)) {
8ca2e9
 			fprintf(stderr, "tiffsplit: strip byte counts are missing\n");
8ca2e9
+			_TIFFfree(buf);
8ca2e9
 			return (0);
8ca2e9
 		}
8ca2e9
 		for (s = 0; s < ns; s++) {
8ca2e9
@@ -272,6 +273,7 @@ cpTiles(TIFF* in, TIFF* out)
8ca2e9
 
8ca2e9
 		if (!TIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &bytecounts)) {
8ca2e9
 			fprintf(stderr, "tiffsplit: tile byte counts are missing\n");
8ca2e9
+			_TIFFfree(buf);
8ca2e9
 			return (0);
8ca2e9
 		}
8ca2e9
 		for (t = 0; t < nt; t++) {
8ca2e9
-- 
8ca2e9
2.21.0
8ca2e9