From e18809068d534d9954b0fe21939d8d7ecf5342c9 Mon Sep 17 00:00:00 2001 From: Bob Friesenhahn Date: Sat, 30 May 2015 16:13:29 +0000 Subject: [PATCH] * tools/raw2tiff.c (main): Fix Coverity 1024887 "Unchecked return value from library". (guessSize): Fix Coverity 1024888 "Unchecked return value from library". (guessSize): Fix Coverity 1214162 "Ignoring number of bytes read". (guessSize): Fix Coverity 1024889 "Unchecked return value from library". --- ChangeLog | 8 +++++ tools/raw2tiff.c | 78 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 62 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index ad41263e..ef74d26a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2015-05-30 Bob Friesenhahn + * tools/raw2tiff.c (main): Fix Coverity 1024887 "Unchecked return + value from library". + (guessSize): Fix Coverity 1024888 "Unchecked return value from + library". + (guessSize): Fix Coverity 1214162 "Ignoring number of bytes read". + (guessSize): Fix Coverity 1024889 "Unchecked return value from + library". + * tools/tiff2pdf.c (t2p_readwrite_pdf_image): Fix Coverity 298621 "Resource leak". (t2p_readwrite_pdf_image): Fix Coverity 1024181 "Structurally dead diff --git a/tools/raw2tiff.c b/tools/raw2tiff.c index 9ab980e5..5463f3bd 100644 --- a/tools/raw2tiff.c +++ b/tools/raw2tiff.c @@ -1,4 +1,4 @@ -/* $Id: raw2tiff.c,v 1.25 2010-03-10 18:56:49 bfriesen Exp $ +/* $Id: raw2tiff.c,v 1.26 2015-05-30 16:13:29 bfriesen Exp $ * * Project: libtiff tools * Purpose: Convert raw byte sequences in TIFF images @@ -286,15 +286,21 @@ main(int argc, char* argv[]) switch(interleaving) { case BAND: /* band interleaved data */ for (band = 0; band < nbands; band++) { - lseek(fd, - hdr_size + (length*band+row)*linebytes, - SEEK_SET); + if (lseek(fd, + hdr_size + (length*band+row)*linebytes, + SEEK_SET) == (off_t)-1) { + fprintf(stderr, + "%s: %s: scanline %lu: seek error.\n", + argv[0], argv[optind], + (unsigned long) row); + break; + } if (read(fd, buf, linebytes) < 0) { fprintf(stderr, - "%s: %s: scanline %lu: Read error.\n", - argv[0], argv[optind], - (unsigned long) row); - break; + "%s: %s: scanline %lu: Read error.\n", + argv[0], argv[optind], + (unsigned long) row); + break; } if (swab) /* Swap bytes if needed */ swapBytesInScanline(buf, width, dtype); @@ -366,7 +372,10 @@ guessSize(int fd, TIFFDataType dtype, off_t hdr_size, uint32 nbands, uint32 depth = TIFFDataWidth(dtype); float cor_coef = 0, tmp; - fstat(fd, &filestat); + if (fstat(fd, &filestat) == -1) { + fprintf(stderr, "Failed to obtain file size.\n"); + return -1; + } if (filestat.st_size < hdr_size) { fprintf(stderr, "Too large header size specified.\n"); @@ -394,6 +403,7 @@ guessSize(int fd, TIFFDataType dtype, off_t hdr_size, uint32 nbands, return 1; } else if (*width == 0 && *length == 0) { + unsigned int fail = 0; fprintf(stderr, "Image width and height are not specified.\n"); for (w = (uint32) sqrt(imagesize / longt); @@ -404,26 +414,46 @@ guessSize(int fd, TIFFDataType dtype, off_t hdr_size, uint32 nbands, buf1 = _TIFFmalloc(scanlinesize); buf2 = _TIFFmalloc(scanlinesize); h = imagesize / w; - lseek(fd, hdr_size + (int)(h/2)*scanlinesize, - SEEK_SET); - read(fd, buf1, scanlinesize); - read(fd, buf2, scanlinesize); - if (swab) { - swapBytesInScanline(buf1, w, dtype); - swapBytesInScanline(buf2, w, dtype); - } - tmp = (float) fabs(correlation(buf1, buf2, - w, dtype)); - if (tmp > cor_coef) { - cor_coef = tmp; - *width = w, *length = h; - } + do { + if (lseek(fd, hdr_size + (int)(h/2)*scanlinesize, + SEEK_SET) == (off_t)-1) { + fprintf(stderr, "seek error.\n"); + fail=1; + break; + } + if (read(fd, buf1, scanlinesize) != + (long) scanlinesize) { + fprintf(stderr, "read error.\n"); + fail=1; + break; + } + if (read(fd, buf2, scanlinesize) != + (long) scanlinesize) { + fprintf(stderr, "read error.\n"); + fail=1; + break; + } + if (swab) { + swapBytesInScanline(buf1, w, dtype); + swapBytesInScanline(buf2, w, dtype); + } + tmp = (float) fabs(correlation(buf1, buf2, + w, dtype)); + if (tmp > cor_coef) { + cor_coef = tmp; + *width = w, *length = h; + } + } while (0); - _TIFFfree(buf1); + _TIFFfree(buf1); _TIFFfree(buf2); } } + if (fail) { + return -1; + } + fprintf(stderr, "Width is guessed as %lu, height is guessed as %lu.\n", (unsigned long)*width, (unsigned long)*length);