303 lines
9.3 KiB
Diff
303 lines
9.3 KiB
Diff
From bf4ca0b5a627bdc00f51d85184beaac6318be355 Mon Sep 17 00:00:00 2001
|
|
From: erouault <erouault>
|
|
Date: Wed, 11 Jan 2017 12:51:59 +0000
|
|
Subject: [PATCH 5/8] * libtiff/tif_dirwrite.c: in
|
|
TIFFWriteDirectoryTagCheckedRational, replace assertion by runtime check to
|
|
error out if passed value is strictly negative. Fixes
|
|
http://bugzilla.maptools.org/show_bug.cgi?id=2535
|
|
|
|
* tools/tiffcrop.c: remove extraneous TIFFClose() in error code path, that
|
|
caused double free.
|
|
Related to http://bugzilla.maptools.org/show_bug.cgi?id=2535
|
|
|
|
* libtiff/tif_dir.c, tif_dirread.c, tif_dirwrite.c: implement various clampings of double to other data types to avoid undefined behaviour if the output range isn't big enough to hold the input value. Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2643 http://bugzilla.maptools.org/show_bug.cgi?id=2642 http://bugzilla.maptools.org/show_bug.cgi?id=2646 http://bugzilla.maptools.org/show_bug.cgi?id=2647
|
|
---
|
|
libtiff/tif_dir.c | 18 +++++++--
|
|
libtiff/tif_dirread.c | 10 ++++-
|
|
libtiff/tif_dirwrite.c | 99 ++++++++++++++++++++++++++++++++++++++++++++------
|
|
tools/tiffcrop.c | 1 -
|
|
4 files changed, 110 insertions(+), 18 deletions(-)
|
|
|
|
diff --git a/libtiff/tif_dir.c b/libtiff/tif_dir.c
|
|
index ad21655..8806241 100644
|
|
--- a/libtiff/tif_dir.c
|
|
+++ b/libtiff/tif_dir.c
|
|
@@ -31,6 +31,7 @@
|
|
* (and also some miscellaneous stuff)
|
|
*/
|
|
#include "tiffiop.h"
|
|
+#include <float.h>
|
|
|
|
/*
|
|
* These are used in the backwards compatibility code...
|
|
@@ -154,6 +155,15 @@ bad:
|
|
return (0);
|
|
}
|
|
|
|
+static float TIFFClampDoubleToFloat( double val )
|
|
+{
|
|
+ if( val > FLT_MAX )
|
|
+ return FLT_MAX;
|
|
+ if( val < -FLT_MAX )
|
|
+ return -FLT_MAX;
|
|
+ return (float)val;
|
|
+}
|
|
+
|
|
static int
|
|
_TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
|
|
{
|
|
@@ -312,13 +322,13 @@ _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
|
|
dblval = va_arg(ap, double);
|
|
if( dblval < 0 )
|
|
goto badvaluedouble;
|
|
- td->td_xresolution = (float) dblval;
|
|
+ td->td_xresolution = TIFFClampDoubleToFloat( dblval );
|
|
break;
|
|
case TIFFTAG_YRESOLUTION:
|
|
dblval = va_arg(ap, double);
|
|
if( dblval < 0 )
|
|
goto badvaluedouble;
|
|
- td->td_yresolution = (float) dblval;
|
|
+ td->td_yresolution = TIFFClampDoubleToFloat( dblval );
|
|
break;
|
|
case TIFFTAG_PLANARCONFIG:
|
|
v = (uint16) va_arg(ap, uint16_vap);
|
|
@@ -327,10 +337,10 @@ _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
|
|
td->td_planarconfig = (uint16) v;
|
|
break;
|
|
case TIFFTAG_XPOSITION:
|
|
- td->td_xposition = (float) va_arg(ap, double);
|
|
+ td->td_xposition = TIFFClampDoubleToFloat( va_arg(ap, double) );
|
|
break;
|
|
case TIFFTAG_YPOSITION:
|
|
- td->td_yposition = (float) va_arg(ap, double);
|
|
+ td->td_yposition = TIFFClampDoubleToFloat( va_arg(ap, double) );
|
|
break;
|
|
case TIFFTAG_RESOLUTIONUNIT:
|
|
v = (uint16) va_arg(ap, uint16_vap);
|
|
diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
|
|
index bfd0105..7d1d194 100644
|
|
--- a/libtiff/tif_dirread.c
|
|
+++ b/libtiff/tif_dirread.c
|
|
@@ -40,6 +40,7 @@
|
|
*/
|
|
|
|
#include "tiffiop.h"
|
|
+#include <float.h>
|
|
|
|
#define IGNORE 0 /* tag placeholder used below */
|
|
#define FAILED_FII ((uint32) -1)
|
|
@@ -2406,7 +2407,14 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEnt
|
|
ma=(double*)origdata;
|
|
mb=data;
|
|
for (n=0; n<count; n++)
|
|
- *mb++=(float)(*ma++);
|
|
+ {
|
|
+ double val = *ma++;
|
|
+ if( val > FLT_MAX )
|
|
+ val = FLT_MAX;
|
|
+ else if( val < -FLT_MAX )
|
|
+ val = -FLT_MAX;
|
|
+ *mb++=(float)val;
|
|
+ }
|
|
}
|
|
break;
|
|
}
|
|
diff --git a/libtiff/tif_dirwrite.c b/libtiff/tif_dirwrite.c
|
|
index d34f6f6..50bc3d5 100644
|
|
--- a/libtiff/tif_dirwrite.c
|
|
+++ b/libtiff/tif_dirwrite.c
|
|
@@ -30,6 +30,7 @@
|
|
* Directory Write Support Routines.
|
|
*/
|
|
#include "tiffiop.h"
|
|
+#include <float.h>
|
|
|
|
#ifdef HAVE_IEEEFP
|
|
#define TIFFCvtNativeToIEEEFloat(tif, n, fp)
|
|
@@ -939,6 +940,69 @@ bad:
|
|
return(0);
|
|
}
|
|
|
|
+static float TIFFClampDoubleToFloat( double val )
|
|
+{
|
|
+ if( val > FLT_MAX )
|
|
+ return FLT_MAX;
|
|
+ if( val < -FLT_MAX )
|
|
+ return -FLT_MAX;
|
|
+ return (float)val;
|
|
+}
|
|
+
|
|
+static int8 TIFFClampDoubleToInt8( double val )
|
|
+{
|
|
+ if( val > 127 )
|
|
+ return 127;
|
|
+ if( val < -128 || val != val )
|
|
+ return -128;
|
|
+ return (int8)val;
|
|
+}
|
|
+
|
|
+static int16 TIFFClampDoubleToInt16( double val )
|
|
+{
|
|
+ if( val > 32767 )
|
|
+ return 32767;
|
|
+ if( val < -32768 || val != val )
|
|
+ return -32768;
|
|
+ return (int16)val;
|
|
+}
|
|
+
|
|
+static int32 TIFFClampDoubleToInt32( double val )
|
|
+{
|
|
+ if( val > 0x7FFFFFFF )
|
|
+ return 0x7FFFFFFF;
|
|
+ if( val < -0x7FFFFFFF-1 || val != val )
|
|
+ return -0x7FFFFFFF-1;
|
|
+ return (int32)val;
|
|
+}
|
|
+
|
|
+static uint8 TIFFClampDoubleToUInt8( double val )
|
|
+{
|
|
+ if( val < 0 )
|
|
+ return 0;
|
|
+ if( val > 255 || val != val )
|
|
+ return 255;
|
|
+ return (uint8)val;
|
|
+}
|
|
+
|
|
+static uint16 TIFFClampDoubleToUInt16( double val )
|
|
+{
|
|
+ if( val < 0 )
|
|
+ return 0;
|
|
+ if( val > 65535 || val != val )
|
|
+ return 65535;
|
|
+ return (uint16)val;
|
|
+}
|
|
+
|
|
+static uint32 TIFFClampDoubleToUInt32( double val )
|
|
+{
|
|
+ if( val < 0 )
|
|
+ return 0;
|
|
+ if( val > 0xFFFFFFFFU || val != val )
|
|
+ return 0xFFFFFFFFU;
|
|
+ return (uint32)val;
|
|
+}
|
|
+
|
|
static int
|
|
TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
|
|
{
|
|
@@ -959,7 +1023,7 @@ TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* di
|
|
if (tif->tif_dir.td_bitspersample<=32)
|
|
{
|
|
for (i = 0; i < count; ++i)
|
|
- ((float*)conv)[i] = (float)value[i];
|
|
+ ((float*)conv)[i] = TIFFClampDoubleToFloat(value[i]);
|
|
ok = TIFFWriteDirectoryTagFloatArray(tif,ndir,dir,tag,count,(float*)conv);
|
|
}
|
|
else
|
|
@@ -971,19 +1035,19 @@ TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* di
|
|
if (tif->tif_dir.td_bitspersample<=8)
|
|
{
|
|
for (i = 0; i < count; ++i)
|
|
- ((int8*)conv)[i] = (int8)value[i];
|
|
+ ((int8*)conv)[i] = TIFFClampDoubleToInt8(value[i]);
|
|
ok = TIFFWriteDirectoryTagSbyteArray(tif,ndir,dir,tag,count,(int8*)conv);
|
|
}
|
|
else if (tif->tif_dir.td_bitspersample<=16)
|
|
{
|
|
for (i = 0; i < count; ++i)
|
|
- ((int16*)conv)[i] = (int16)value[i];
|
|
+ ((int16*)conv)[i] = TIFFClampDoubleToInt16(value[i]);
|
|
ok = TIFFWriteDirectoryTagSshortArray(tif,ndir,dir,tag,count,(int16*)conv);
|
|
}
|
|
else
|
|
{
|
|
for (i = 0; i < count; ++i)
|
|
- ((int32*)conv)[i] = (int32)value[i];
|
|
+ ((int32*)conv)[i] = TIFFClampDoubleToInt32(value[i]);
|
|
ok = TIFFWriteDirectoryTagSlongArray(tif,ndir,dir,tag,count,(int32*)conv);
|
|
}
|
|
break;
|
|
@@ -991,19 +1055,19 @@ TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* di
|
|
if (tif->tif_dir.td_bitspersample<=8)
|
|
{
|
|
for (i = 0; i < count; ++i)
|
|
- ((uint8*)conv)[i] = (uint8)value[i];
|
|
+ ((uint8*)conv)[i] = TIFFClampDoubleToUInt8(value[i]);
|
|
ok = TIFFWriteDirectoryTagByteArray(tif,ndir,dir,tag,count,(uint8*)conv);
|
|
}
|
|
else if (tif->tif_dir.td_bitspersample<=16)
|
|
{
|
|
for (i = 0; i < count; ++i)
|
|
- ((uint16*)conv)[i] = (uint16)value[i];
|
|
+ ((uint16*)conv)[i] = TIFFClampDoubleToUInt16(value[i]);
|
|
ok = TIFFWriteDirectoryTagShortArray(tif,ndir,dir,tag,count,(uint16*)conv);
|
|
}
|
|
else
|
|
{
|
|
for (i = 0; i < count; ++i)
|
|
- ((uint32*)conv)[i] = (uint32)value[i];
|
|
+ ((uint32*)conv)[i] = TIFFClampDoubleToUInt32(value[i]);
|
|
ok = TIFFWriteDirectoryTagLongArray(tif,ndir,dir,tag,count,(uint32*)conv);
|
|
}
|
|
break;
|
|
@@ -2094,15 +2158,25 @@ TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* d
|
|
static int
|
|
TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
|
|
{
|
|
+ static const char module[] = "TIFFWriteDirectoryTagCheckedRational";
|
|
uint32 m[2];
|
|
- assert(value>=0.0);
|
|
assert(sizeof(uint32)==4);
|
|
- if (value<=0.0)
|
|
+ if( value < 0 )
|
|
+ {
|
|
+ TIFFErrorExt(tif->tif_clientdata,module,"Negative value is illegal");
|
|
+ return 0;
|
|
+ }
|
|
+ else if( value != value )
|
|
+ {
|
|
+ TIFFErrorExt(tif->tif_clientdata,module,"Not-a-number value is illegal");
|
|
+ return 0;
|
|
+ }
|
|
+ else if (value==0.0)
|
|
{
|
|
m[0]=0;
|
|
m[1]=1;
|
|
}
|
|
- else if (value==(double)(uint32)value)
|
|
+ else if (value <= 0xFFFFFFFFU && value==(double)(uint32)value)
|
|
{
|
|
m[0]=(uint32)value;
|
|
m[1]=1;
|
|
@@ -2143,12 +2217,13 @@ TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry*
|
|
}
|
|
for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
|
|
{
|
|
- if (*na<=0.0)
|
|
+ if (*na<=0.0 || *na != *na)
|
|
{
|
|
nb[0]=0;
|
|
nb[1]=1;
|
|
}
|
|
- else if (*na==(float)(uint32)(*na))
|
|
+ else if (*na >= 0 && *na <= (float)0xFFFFFFFFU &&
|
|
+ *na==(float)(uint32)(*na))
|
|
{
|
|
nb[0]=(uint32)(*na);
|
|
nb[1]=1;
|
|
diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
|
|
index ad2c00f..6e7b727 100644
|
|
--- a/tools/tiffcrop.c
|
|
+++ b/tools/tiffcrop.c
|
|
@@ -7986,7 +7986,6 @@ writeCroppedImage(TIFF *in, TIFF *out, struct image_data *image,
|
|
if (!TIFFWriteDirectory(out))
|
|
{
|
|
TIFFError("","Failed to write IFD for page number %d", pagenum);
|
|
- TIFFClose(out);
|
|
return (-1);
|
|
}
|
|
|
|
--
|
|
2.7.4
|
|
|