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