Index: tiff-3.8.2/libtiff/tif_dir.c
===================================================================
--- tiff-3.8.2.orig/libtiff/tif_dir.c	2018-05-15 11:00:43.012638909 +0200
+++ tiff-3.8.2/libtiff/tif_dir.c	2018-05-15 11:00:43.052639467 +0200
@@ -31,6 +31,7 @@
  * (and also some miscellaneous stuff)
  */
 #include "tiffiop.h"
+#include <float.h>
 
 /*
  * These are used in the backwards compatibility code...
@@ -117,6 +118,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, ttag_t tag, va_list ap)
 {
@@ -236,13 +246,13 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va
         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 = va_arg(ap, uint32);
@@ -251,10 +261,10 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va
 		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 = va_arg(ap, uint32);

Index: tiff-3.8.2/libtiff/tif_dirwrite.c
===================================================================
--- tiff-3.8.2.orig/libtiff/tif_dirwrite.c	2006-03-03 13:09:43.000000000 +0100
+++ tiff-3.8.2/libtiff/tif_dirwrite.c	2018-05-15 12:58:20.253544972 +0200
@@ -30,6 +30,7 @@
  * Directory Write Support Routines.
  */
 #include "tiffiop.h"
+#include <float.h>
 
 #ifdef HAVE_IEEEFP
 # define	TIFFCvtNativeToIEEEFloat(tif, n, fp)
@@ -886,6 +887,69 @@ TIFFWriteFloatArray(TIFF* tif, TIFFDirEn
 		return (TIFFWriteData(tif, dir, (char*) v));
 }
 
+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
 TIFFWriteDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v)
 {
@@ -928,7 +992,7 @@ TIFFWriteAnyArray(TIFF* tif,
 		{ 
 			uint8* bp = (uint8*) w;
 			for (i = 0; i < (int) n; i++)
-				bp[i] = (uint8) v[i];
+				bp[i] = TIFFClampDoubleToUInt8(v[i]);
 			if (!TIFFWriteByteArray(tif, dir, (char*) bp))
 				goto out;
 		}
@@ -937,7 +1001,7 @@ TIFFWriteAnyArray(TIFF* tif,
 		{ 
 			int8* bp = (int8*) w;
 			for (i = 0; i < (int) n; i++)
-				bp[i] = (int8) v[i];
+				bp[i] = TIFFClampDoubleToInt8(v[i]);
 			if (!TIFFWriteByteArray(tif, dir, (char*) bp))
 				goto out;
 		}
@@ -946,7 +1010,7 @@ TIFFWriteAnyArray(TIFF* tif,
 		{
 			uint16* bp = (uint16*) w;
 			for (i = 0; i < (int) n; i++)
-				bp[i] = (uint16) v[i];
+				bp[i] = TIFFClampDoubleToUInt16(v[i]);
 			if (!TIFFWriteShortArray(tif, dir, (uint16*)bp))
 				goto out;
 		}
@@ -955,7 +1019,7 @@ TIFFWriteAnyArray(TIFF* tif,
 		{ 
 			int16* bp = (int16*) w;
 			for (i = 0; i < (int) n; i++)
-				bp[i] = (int16) v[i];
+				bp[i] = TIFFClampDoubleToInt16(v[i]);
 			if (!TIFFWriteShortArray(tif, dir, (uint16*)bp))
 				goto out;
 		}
@@ -964,7 +1028,7 @@ TIFFWriteAnyArray(TIFF* tif,
 		{
 			uint32* bp = (uint32*) w;
 			for (i = 0; i < (int) n; i++)
-				bp[i] = (uint32) v[i];
+				bp[i] =  TIFFClampDoubleToUInt32(v[i]);
 			if (!TIFFWriteLongArray(tif, dir, bp))
 				goto out;
 		}
@@ -973,7 +1037,7 @@ TIFFWriteAnyArray(TIFF* tif,
 		{
 			int32* bp = (int32*) w;
 			for (i = 0; i < (int) n; i++)
-				bp[i] = (int32) v[i];
+				bp[i] = TIFFClampDoubleToInt32(v[i]);
 			if (!TIFFWriteLongArray(tif, dir, (uint32*) bp))
 				goto out;
 		}
@@ -982,7 +1046,7 @@ TIFFWriteAnyArray(TIFF* tif,
 		{ 
 			float* bp = (float*) w;
 			for (i = 0; i < (int) n; i++)
-				bp[i] = (float) v[i];
+				bp[i] = TIFFClampDoubleToFloat(v[i]);
 			if (!TIFFWriteFloatArray(tif, dir, bp))
 				goto out;
 		}
