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