You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
9.3 KiB

  1. From 3144e57770c1e4d26520d8abee750f8ac8b75490 Mon Sep 17 00:00:00 2001
  2. From: erouault <erouault>
  3. Date: Wed, 11 Jan 2017 16:09:02 +0000
  4. Subject: [PATCH] * libtiff/tif_dir.c, tif_dirread.c, tif_dirwrite.c: implement
  5. various clampings of double to other data types to avoid undefined behaviour
  6. if the output range isn't big enough to hold the input value. Fixes
  7. http://bugzilla.maptools.org/show_bug.cgi?id=2643
  8. http://bugzilla.maptools.org/show_bug.cgi?id=2642
  9. http://bugzilla.maptools.org/show_bug.cgi?id=2646
  10. http://bugzilla.maptools.org/show_bug.cgi?id=2647
  11. ---
  12. ChangeLog | 10 ++++++
  13. libtiff/tif_dir.c | 18 +++++++---
  14. libtiff/tif_dirread.c | 10 +++++-
  15. libtiff/tif_dirwrite.c | 90 ++++++++++++++++++++++++++++++++++++++++++++------
  16. 4 files changed, 113 insertions(+), 15 deletions(-)
  17. diff --git a/ChangeLog b/ChangeLog
  18. index 722a405..6517640 100644
  19. --- a/ChangeLog
  20. +++ b/ChangeLog
  21. @@ -1,5 +1,15 @@
  22. 2017-01-11 Even Rouault <even.rouault at spatialys.com>
  23. + * libtiff/tif_dir.c, tif_dirread.c, tif_dirwrite.c: implement various clampings
  24. + of double to other data types to avoid undefined behaviour if the output range
  25. + isn't big enough to hold the input value.
  26. + Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2643
  27. + http://bugzilla.maptools.org/show_bug.cgi?id=2642
  28. + http://bugzilla.maptools.org/show_bug.cgi?id=2646
  29. + http://bugzilla.maptools.org/show_bug.cgi?id=2647
  30. +
  31. +2017-01-11 Even Rouault <even.rouault at spatialys.com>
  32. +
  33. * libtiff/tif_dirread.c: avoid division by floating point 0 in
  34. TIFFReadDirEntryCheckedRational() and TIFFReadDirEntryCheckedSrational(),
  35. and return 0 in that case (instead of infinity as before presumably)
  36. diff --git a/libtiff/tif_dir.c b/libtiff/tif_dir.c
  37. index 68a55af..a04d28f 100644
  38. --- a/libtiff/tif_dir.c
  39. +++ b/libtiff/tif_dir.c
  40. @@ -31,6 +31,7 @@
  41. * (and also some miscellaneous stuff)
  42. */
  43. #include "tiffiop.h"
  44. +#include <float.h>
  45. /*
  46. * These are used in the backwards compatibility code...
  47. @@ -154,6 +155,15 @@ checkInkNamesString(TIFF* tif, uint32 slen, const char* s)
  48. return (0);
  49. }
  50. +static float TIFFClampDoubleToFloat( double val )
  51. +{
  52. + if( val > FLT_MAX )
  53. + return FLT_MAX;
  54. + if( val < -FLT_MAX )
  55. + return -FLT_MAX;
  56. + return (float)val;
  57. +}
  58. +
  59. static int
  60. _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
  61. {
  62. @@ -312,13 +322,13 @@ _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
  63. dblval = va_arg(ap, double);
  64. if( dblval < 0 )
  65. goto badvaluedouble;
  66. - td->td_xresolution = (float) dblval;
  67. + td->td_xresolution = TIFFClampDoubleToFloat( dblval );
  68. break;
  69. case TIFFTAG_YRESOLUTION:
  70. dblval = va_arg(ap, double);
  71. if( dblval < 0 )
  72. goto badvaluedouble;
  73. - td->td_yresolution = (float) dblval;
  74. + td->td_yresolution = TIFFClampDoubleToFloat( dblval );
  75. break;
  76. case TIFFTAG_PLANARCONFIG:
  77. v = (uint16) va_arg(ap, uint16_vap);
  78. @@ -327,10 +337,10 @@ _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
  79. td->td_planarconfig = (uint16) v;
  80. break;
  81. case TIFFTAG_XPOSITION:
  82. - td->td_xposition = (float) va_arg(ap, double);
  83. + td->td_xposition = TIFFClampDoubleToFloat( va_arg(ap, double) );
  84. break;
  85. case TIFFTAG_YPOSITION:
  86. - td->td_yposition = (float) va_arg(ap, double);
  87. + td->td_yposition = TIFFClampDoubleToFloat( va_arg(ap, double) );
  88. break;
  89. case TIFFTAG_RESOLUTIONUNIT:
  90. v = (uint16) va_arg(ap, uint16_vap);
  91. diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
  92. index 8a1e42a..77b0f37 100644
  93. --- a/libtiff/tif_dirread.c
  94. +++ b/libtiff/tif_dirread.c
  95. @@ -40,6 +40,7 @@
  96. */
  97. #include "tiffiop.h"
  98. +#include <float.h>
  99. #define IGNORE 0 /* tag placeholder used below */
  100. #define FAILED_FII ((uint32) -1)
  101. @@ -2406,7 +2407,14 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEnt
  102. ma=(double*)origdata;
  103. mb=data;
  104. for (n=0; n<count; n++)
  105. - *mb++=(float)(*ma++);
  106. + {
  107. + double val = *ma++;
  108. + if( val > FLT_MAX )
  109. + val = FLT_MAX;
  110. + else if( val < -FLT_MAX )
  111. + val = -FLT_MAX;
  112. + *mb++=(float)val;
  113. + }
  114. }
  115. break;
  116. }
  117. diff --git a/libtiff/tif_dirwrite.c b/libtiff/tif_dirwrite.c
  118. index c9e871b..2967da5 100644
  119. --- a/libtiff/tif_dirwrite.c
  120. +++ b/libtiff/tif_dirwrite.c
  121. @@ -30,6 +30,7 @@
  122. * Directory Write Support Routines.
  123. */
  124. #include "tiffiop.h"
  125. +#include <float.h>
  126. #ifdef HAVE_IEEEFP
  127. #define TIFFCvtNativeToIEEEFloat(tif, n, fp)
  128. @@ -939,6 +940,69 @@ TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)
  129. return(0);
  130. }
  131. +static float TIFFClampDoubleToFloat( double val )
  132. +{
  133. + if( val > FLT_MAX )
  134. + return FLT_MAX;
  135. + if( val < -FLT_MAX )
  136. + return -FLT_MAX;
  137. + return (float)val;
  138. +}
  139. +
  140. +static int8 TIFFClampDoubleToInt8( double val )
  141. +{
  142. + if( val > 127 )
  143. + return 127;
  144. + if( val < -128 || val != val )
  145. + return -128;
  146. + return (int8)val;
  147. +}
  148. +
  149. +static int16 TIFFClampDoubleToInt16( double val )
  150. +{
  151. + if( val > 32767 )
  152. + return 32767;
  153. + if( val < -32768 || val != val )
  154. + return -32768;
  155. + return (int16)val;
  156. +}
  157. +
  158. +static int32 TIFFClampDoubleToInt32( double val )
  159. +{
  160. + if( val > 0x7FFFFFFF )
  161. + return 0x7FFFFFFF;
  162. + if( val < -0x7FFFFFFF-1 || val != val )
  163. + return -0x7FFFFFFF-1;
  164. + return (int32)val;
  165. +}
  166. +
  167. +static uint8 TIFFClampDoubleToUInt8( double val )
  168. +{
  169. + if( val < 0 )
  170. + return 0;
  171. + if( val > 255 || val != val )
  172. + return 255;
  173. + return (uint8)val;
  174. +}
  175. +
  176. +static uint16 TIFFClampDoubleToUInt16( double val )
  177. +{
  178. + if( val < 0 )
  179. + return 0;
  180. + if( val > 65535 || val != val )
  181. + return 65535;
  182. + return (uint16)val;
  183. +}
  184. +
  185. +static uint32 TIFFClampDoubleToUInt32( double val )
  186. +{
  187. + if( val < 0 )
  188. + return 0;
  189. + if( val > 0xFFFFFFFFU || val != val )
  190. + return 0xFFFFFFFFU;
  191. + return (uint32)val;
  192. +}
  193. +
  194. static int
  195. TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
  196. {
  197. @@ -959,7 +1023,7 @@ TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* di
  198. if (tif->tif_dir.td_bitspersample<=32)
  199. {
  200. for (i = 0; i < count; ++i)
  201. - ((float*)conv)[i] = (float)value[i];
  202. + ((float*)conv)[i] = TIFFClampDoubleToFloat(value[i]);
  203. ok = TIFFWriteDirectoryTagFloatArray(tif,ndir,dir,tag,count,(float*)conv);
  204. }
  205. else
  206. @@ -971,19 +1035,19 @@ TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* di
  207. if (tif->tif_dir.td_bitspersample<=8)
  208. {
  209. for (i = 0; i < count; ++i)
  210. - ((int8*)conv)[i] = (int8)value[i];
  211. + ((int8*)conv)[i] = TIFFClampDoubleToInt8(value[i]);
  212. ok = TIFFWriteDirectoryTagSbyteArray(tif,ndir,dir,tag,count,(int8*)conv);
  213. }
  214. else if (tif->tif_dir.td_bitspersample<=16)
  215. {
  216. for (i = 0; i < count; ++i)
  217. - ((int16*)conv)[i] = (int16)value[i];
  218. + ((int16*)conv)[i] = TIFFClampDoubleToInt16(value[i]);
  219. ok = TIFFWriteDirectoryTagSshortArray(tif,ndir,dir,tag,count,(int16*)conv);
  220. }
  221. else
  222. {
  223. for (i = 0; i < count; ++i)
  224. - ((int32*)conv)[i] = (int32)value[i];
  225. + ((int32*)conv)[i] = TIFFClampDoubleToInt32(value[i]);
  226. ok = TIFFWriteDirectoryTagSlongArray(tif,ndir,dir,tag,count,(int32*)conv);
  227. }
  228. break;
  229. @@ -991,19 +1055,19 @@ TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* di
  230. if (tif->tif_dir.td_bitspersample<=8)
  231. {
  232. for (i = 0; i < count; ++i)
  233. - ((uint8*)conv)[i] = (uint8)value[i];
  234. + ((uint8*)conv)[i] = TIFFClampDoubleToUInt8(value[i]);
  235. ok = TIFFWriteDirectoryTagByteArray(tif,ndir,dir,tag,count,(uint8*)conv);
  236. }
  237. else if (tif->tif_dir.td_bitspersample<=16)
  238. {
  239. for (i = 0; i < count; ++i)
  240. - ((uint16*)conv)[i] = (uint16)value[i];
  241. + ((uint16*)conv)[i] = TIFFClampDoubleToUInt16(value[i]);
  242. ok = TIFFWriteDirectoryTagShortArray(tif,ndir,dir,tag,count,(uint16*)conv);
  243. }
  244. else
  245. {
  246. for (i = 0; i < count; ++i)
  247. - ((uint32*)conv)[i] = (uint32)value[i];
  248. + ((uint32*)conv)[i] = TIFFClampDoubleToUInt32(value[i]);
  249. ok = TIFFWriteDirectoryTagLongArray(tif,ndir,dir,tag,count,(uint32*)conv);
  250. }
  251. break;
  252. @@ -2094,6 +2094,7 @@ TIFFWriteDirectoryTagCheckedSlong8Array(
  253. static int
  254. TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
  255. {
  256. + static const char module[] = "TIFFWriteDirectoryTagCheckedRational";
  257. uint32 m[2];
  258. assert(value>=0.0);
  259. assert(sizeof(uint32)==4);
  260. @@ -2102,7 +2102,12 @@ TIFFWriteDirectoryTagCheckedRational(TIF
  261. m[0]=0;
  262. m[1]=1;
  263. }
  264. - else if (value==(double)(uint32)value)
  265. + else if( value != value )
  266. + {
  267. + TIFFErrorExt(tif->tif_clientdata,module,"Not-a-number value is illegal");
  268. + return 0;
  269. + }
  270. + else if (value <= 0xFFFFFFFFU && value==(double)(uint32)value)
  271. {
  272. m[0]=(uint32)value;
  273. m[1]=1;
  274. @@ -2143,12 +2212,13 @@ TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry*
  275. }
  276. for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
  277. {
  278. - if (*na<=0.0)
  279. + if (*na<=0.0 || *na != *na)
  280. {
  281. nb[0]=0;
  282. nb[1]=1;
  283. }
  284. - else if (*na==(float)(uint32)(*na))
  285. + else if (*na >= 0 && *na <= (float)0xFFFFFFFFU &&
  286. + *na==(float)(uint32)(*na))
  287. {
  288. nb[0]=(uint32)(*na);
  289. nb[1]=1;