|
| 1 | +From dd1bcc7abb26094e93636e85520f0d8f81ab0fab Mon Sep 17 00:00:00 2001 |
| 2 | +From: 4ugustus <wangdw.augustus@qq.com> |
| 3 | +Date: Sat, 11 Jun 2022 09:31:43 +0000 |
| 4 | +Subject: [PATCH] fix the FPE in tiffcrop (#415, #427, and #428) |
| 5 | + |
| 6 | +--- |
| 7 | + libtiff/tif_aux.c | 9 +++++++ |
| 8 | + libtiff/tiffiop.h | 1 + |
| 9 | + tools/tiffcrop.c | 62 ++++++++++++++++++++++++++--------------------- |
| 10 | + 3 files changed, 44 insertions(+), 28 deletions(-) |
| 11 | + |
| 12 | +diff --git a/libtiff/tif_aux.c b/libtiff/tif_aux.c |
| 13 | +index 140f26c7..5b88c8d0 100644 |
| 14 | +--- a/libtiff/tif_aux.c |
| 15 | ++++ b/libtiff/tif_aux.c |
| 16 | +@@ -402,6 +402,15 @@ float _TIFFClampDoubleToFloat( double val ) |
| 17 | + return (float)val; |
| 18 | + } |
| 19 | + |
| 20 | ++uint32_t _TIFFClampDoubleToUInt32(double val) |
| 21 | ++{ |
| 22 | ++ if( val < 0 ) |
| 23 | ++ return 0; |
| 24 | ++ if( val > 0xFFFFFFFFU || val != val ) |
| 25 | ++ return 0xFFFFFFFFU; |
| 26 | ++ return (uint32_t)val; |
| 27 | ++} |
| 28 | ++ |
| 29 | + int _TIFFSeekOK(TIFF* tif, toff_t off) |
| 30 | + { |
| 31 | + /* Huge offsets, especially -1 / UINT64_MAX, can cause issues */ |
| 32 | +diff --git a/libtiff/tiffiop.h b/libtiff/tiffiop.h |
| 33 | +index e3af461d..4e8bdac2 100644 |
| 34 | +--- a/libtiff/tiffiop.h |
| 35 | ++++ b/libtiff/tiffiop.h |
| 36 | +@@ -365,6 +365,7 @@ extern double _TIFFUInt64ToDouble(uint64_t); |
| 37 | + extern float _TIFFUInt64ToFloat(uint64_t); |
| 38 | + |
| 39 | + extern float _TIFFClampDoubleToFloat(double); |
| 40 | ++extern uint32_t _TIFFClampDoubleToUInt32(double); |
| 41 | + |
| 42 | + extern tmsize_t |
| 43 | + _TIFFReadEncodedStripAndAllocBuffer(TIFF* tif, uint32_t strip, |
| 44 | +diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c |
| 45 | +index 1f827b2b..90286a5e 100644 |
| 46 | +--- a/tools/tiffcrop.c |
| 47 | ++++ b/tools/tiffcrop.c |
| 48 | +@@ -5268,17 +5268,17 @@ computeInputPixelOffsets(struct crop_mask *crop, struct image_data *image, |
| 49 | + { |
| 50 | + if ((crop->res_unit == RESUNIT_INCH) || (crop->res_unit == RESUNIT_CENTIMETER)) |
| 51 | + { |
| 52 | +- x1 = (uint32_t) (crop->corners[i].X1 * scale * xres); |
| 53 | +- x2 = (uint32_t) (crop->corners[i].X2 * scale * xres); |
| 54 | +- y1 = (uint32_t) (crop->corners[i].Y1 * scale * yres); |
| 55 | +- y2 = (uint32_t) (crop->corners[i].Y2 * scale * yres); |
| 56 | ++ x1 = _TIFFClampDoubleToUInt32(crop->corners[i].X1 * scale * xres); |
| 57 | ++ x2 = _TIFFClampDoubleToUInt32(crop->corners[i].X2 * scale * xres); |
| 58 | ++ y1 = _TIFFClampDoubleToUInt32(crop->corners[i].Y1 * scale * yres); |
| 59 | ++ y2 = _TIFFClampDoubleToUInt32(crop->corners[i].Y2 * scale * yres); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | +- x1 = (uint32_t) (crop->corners[i].X1); |
| 64 | +- x2 = (uint32_t) (crop->corners[i].X2); |
| 65 | +- y1 = (uint32_t) (crop->corners[i].Y1); |
| 66 | +- y2 = (uint32_t) (crop->corners[i].Y2); |
| 67 | ++ x1 = _TIFFClampDoubleToUInt32(crop->corners[i].X1); |
| 68 | ++ x2 = _TIFFClampDoubleToUInt32(crop->corners[i].X2); |
| 69 | ++ y1 = _TIFFClampDoubleToUInt32(crop->corners[i].Y1); |
| 70 | ++ y2 = _TIFFClampDoubleToUInt32(crop->corners[i].Y2); |
| 71 | + } |
| 72 | + /* a) Region needs to be within image sizes 0.. width-1; 0..length-1 |
| 73 | + * b) Corners are expected to be submitted as top-left to bottom-right. |
| 74 | +@@ -5357,17 +5357,17 @@ computeInputPixelOffsets(struct crop_mask *crop, struct image_data *image, |
| 75 | + { |
| 76 | + if (crop->res_unit != RESUNIT_INCH && crop->res_unit != RESUNIT_CENTIMETER) |
| 77 | + { /* User has specified pixels as reference unit */ |
| 78 | +- tmargin = (uint32_t)(crop->margins[0]); |
| 79 | +- lmargin = (uint32_t)(crop->margins[1]); |
| 80 | +- bmargin = (uint32_t)(crop->margins[2]); |
| 81 | +- rmargin = (uint32_t)(crop->margins[3]); |
| 82 | ++ tmargin = _TIFFClampDoubleToUInt32(crop->margins[0]); |
| 83 | ++ lmargin = _TIFFClampDoubleToUInt32(crop->margins[1]); |
| 84 | ++ bmargin = _TIFFClampDoubleToUInt32(crop->margins[2]); |
| 85 | ++ rmargin = _TIFFClampDoubleToUInt32(crop->margins[3]); |
| 86 | + } |
| 87 | + else |
| 88 | + { /* inches or centimeters specified */ |
| 89 | +- tmargin = (uint32_t)(crop->margins[0] * scale * yres); |
| 90 | +- lmargin = (uint32_t)(crop->margins[1] * scale * xres); |
| 91 | +- bmargin = (uint32_t)(crop->margins[2] * scale * yres); |
| 92 | +- rmargin = (uint32_t)(crop->margins[3] * scale * xres); |
| 93 | ++ tmargin = _TIFFClampDoubleToUInt32(crop->margins[0] * scale * yres); |
| 94 | ++ lmargin = _TIFFClampDoubleToUInt32(crop->margins[1] * scale * xres); |
| 95 | ++ bmargin = _TIFFClampDoubleToUInt32(crop->margins[2] * scale * yres); |
| 96 | ++ rmargin = _TIFFClampDoubleToUInt32(crop->margins[3] * scale * xres); |
| 97 | + } |
| 98 | + |
| 99 | + if ((lmargin + rmargin) > image->width) |
| 100 | +@@ -5397,24 +5397,24 @@ computeInputPixelOffsets(struct crop_mask *crop, struct image_data *image, |
| 101 | + if (crop->res_unit != RESUNIT_INCH && crop->res_unit != RESUNIT_CENTIMETER) |
| 102 | + { |
| 103 | + if (crop->crop_mode & CROP_WIDTH) |
| 104 | +- width = (uint32_t)crop->width; |
| 105 | ++ width = _TIFFClampDoubleToUInt32(crop->width); |
| 106 | + else |
| 107 | + width = image->width - lmargin - rmargin; |
| 108 | + |
| 109 | + if (crop->crop_mode & CROP_LENGTH) |
| 110 | +- length = (uint32_t)crop->length; |
| 111 | ++ length = _TIFFClampDoubleToUInt32(crop->length); |
| 112 | + else |
| 113 | + length = image->length - tmargin - bmargin; |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + if (crop->crop_mode & CROP_WIDTH) |
| 118 | +- width = (uint32_t)(crop->width * scale * image->xres); |
| 119 | ++ width = _TIFFClampDoubleToUInt32(crop->width * scale * image->xres); |
| 120 | + else |
| 121 | + width = image->width - lmargin - rmargin; |
| 122 | + |
| 123 | + if (crop->crop_mode & CROP_LENGTH) |
| 124 | +- length = (uint32_t)(crop->length * scale * image->yres); |
| 125 | ++ length = _TIFFClampDoubleToUInt32(crop->length * scale * image->yres); |
| 126 | + else |
| 127 | + length = image->length - tmargin - bmargin; |
| 128 | + } |
| 129 | +@@ -5868,13 +5868,13 @@ computeOutputPixelOffsets (struct crop_mask *crop, struct image_data *image, |
| 130 | + { |
| 131 | + if (page->res_unit == RESUNIT_INCH || page->res_unit == RESUNIT_CENTIMETER) |
| 132 | + { /* inches or centimeters specified */ |
| 133 | +- hmargin = (uint32_t)(page->hmargin * scale * page->hres * ((image->bps + 7) / 8)); |
| 134 | +- vmargin = (uint32_t)(page->vmargin * scale * page->vres * ((image->bps + 7) / 8)); |
| 135 | ++ hmargin = _TIFFClampDoubleToUInt32(page->hmargin * scale * page->hres * ((image->bps + 7) / 8)); |
| 136 | ++ vmargin = _TIFFClampDoubleToUInt32(page->vmargin * scale * page->vres * ((image->bps + 7) / 8)); |
| 137 | + } |
| 138 | + else |
| 139 | + { /* Otherwise user has specified pixels as reference unit */ |
| 140 | +- hmargin = (uint32_t)(page->hmargin * scale * ((image->bps + 7) / 8)); |
| 141 | +- vmargin = (uint32_t)(page->vmargin * scale * ((image->bps + 7) / 8)); |
| 142 | ++ hmargin = _TIFFClampDoubleToUInt32(page->hmargin * scale * ((image->bps + 7) / 8)); |
| 143 | ++ vmargin = _TIFFClampDoubleToUInt32(page->vmargin * scale * ((image->bps + 7) / 8)); |
| 144 | + } |
| 145 | + |
| 146 | + if ((hmargin * 2.0) > (pwidth * page->hres)) |
| 147 | +@@ -5912,13 +5912,13 @@ computeOutputPixelOffsets (struct crop_mask *crop, struct image_data *image, |
| 148 | + { |
| 149 | + if (page->mode & PAGE_MODE_PAPERSIZE ) |
| 150 | + { |
| 151 | +- owidth = (uint32_t)((pwidth * page->hres) - (hmargin * 2)); |
| 152 | +- olength = (uint32_t)((plength * page->vres) - (vmargin * 2)); |
| 153 | ++ owidth = _TIFFClampDoubleToUInt32((pwidth * page->hres) - (hmargin * 2)); |
| 154 | ++ olength = _TIFFClampDoubleToUInt32((plength * page->vres) - (vmargin * 2)); |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | +- owidth = (uint32_t)(iwidth - (hmargin * 2 * page->hres)); |
| 159 | +- olength = (uint32_t)(ilength - (vmargin * 2 * page->vres)); |
| 160 | ++ owidth = _TIFFClampDoubleToUInt32(iwidth - (hmargin * 2 * page->hres)); |
| 161 | ++ olength = _TIFFClampDoubleToUInt32(ilength - (vmargin * 2 * page->vres)); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | +@@ -5927,6 +5927,12 @@ computeOutputPixelOffsets (struct crop_mask *crop, struct image_data *image, |
| 166 | + if (olength > ilength) |
| 167 | + olength = ilength; |
| 168 | + |
| 169 | ++ if (owidth == 0 || olength == 0) |
| 170 | ++ { |
| 171 | ++ TIFFError("computeOutputPixelOffsets", "Integer overflow when calculating the number of pages"); |
| 172 | ++ exit(EXIT_FAILURE); |
| 173 | ++ } |
| 174 | ++ |
| 175 | + /* Compute the number of pages required for Portrait or Landscape */ |
| 176 | + switch (page->orient) |
| 177 | + { |
| 178 | +-- |
| 179 | +GitLab |
| 180 | + |
0 commit comments