1 CVE: CVE-2022-3570 CVE-2022-3598
2 Upstream-Status: Backport
3 Signed-off-by: Ross Burton <ross.burton@arm.com>
5 From afd7086090dafd3949afd172822cbcec4ed17d56 Mon Sep 17 00:00:00 2001
6 From: Su Laus <sulau@freenet.de>
7 Date: Thu, 13 Oct 2022 14:33:27 +0000
8 Subject: [PATCH] tiffcrop subroutines require a larger buffer (fixes #271,
9 #381, #386, #388, #389, #435)
12 tools/tiffcrop.c | 209 ++++++++++++++++++++++++++---------------------
13 1 file changed, 118 insertions(+), 91 deletions(-)
15 diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
16 index 41a2ea36..deab5feb 100644
17 --- a/tools/tiffcrop.c
18 +++ b/tools/tiffcrop.c
19 @@ -212,6 +212,10 @@ static char tiffcrop_rev_date[] = "26-08-2022";
21 #define TIFF_DIR_MAX 65534
23 +/* Some conversion subroutines require image buffers, which are at least 3 bytes
24 + * larger than the necessary size for the image itself. */
25 +#define NUM_BUFF_OVERSIZE_BYTES 3
27 /* Offsets into buffer for margins and fixed width and length segments */
30 @@ -233,7 +237,7 @@ struct offset {
34 - uint32_t size; /* size of this buffer */
35 + size_t size; /* size of this buffer */
36 unsigned char *buffer; /* address of the allocated buffer */
39 @@ -810,8 +814,8 @@ static int readContigTilesIntoBuffer (TIFF* in, uint8_t* buf,
40 uint32_t dst_rowsize, shift_width;
41 uint32_t bytes_per_sample, bytes_per_pixel;
42 uint32_t trailing_bits, prev_trailing_bits;
43 - uint32_t tile_rowsize = TIFFTileRowSize(in);
44 - uint32_t src_offset, dst_offset;
45 + tmsize_t tile_rowsize = TIFFTileRowSize(in);
46 + tmsize_t src_offset, dst_offset;
47 uint32_t row_offset, col_offset;
48 uint8_t *bufp = (uint8_t*) buf;
49 unsigned char *src = NULL;
50 @@ -861,7 +865,7 @@ static int readContigTilesIntoBuffer (TIFF* in, uint8_t* buf,
51 TIFFError("readContigTilesIntoBuffer", "Integer overflow when calculating buffer size.");
54 - tilebuf = limitMalloc(tile_buffsize + 3);
55 + tilebuf = limitMalloc(tile_buffsize + NUM_BUFF_OVERSIZE_BYTES);
58 tilebuf[tile_buffsize] = 0;
59 @@ -1024,7 +1028,7 @@ static int readSeparateTilesIntoBuffer (TIFF* in, uint8_t *obuf,
60 for (sample = 0; (sample < spp) && (sample < MAX_SAMPLES); sample++)
62 srcbuffs[sample] = NULL;
63 - tbuff = (unsigned char *)limitMalloc(tilesize + 8);
64 + tbuff = (unsigned char *)limitMalloc(tilesize + NUM_BUFF_OVERSIZE_BYTES);
67 TIFFError ("readSeparateTilesIntoBuffer",
68 @@ -1217,7 +1221,8 @@ writeBufferToSeparateStrips (TIFF* out, uint8_t* buf,
70 rowstripsize = rowsperstrip * bytes_per_sample * (width + 1);
72 - obuf = limitMalloc (rowstripsize);
73 + /* Add 3 padding bytes for extractContigSamples32bits */
74 + obuf = limitMalloc (rowstripsize + NUM_BUFF_OVERSIZE_BYTES);
78 @@ -1229,7 +1234,7 @@ writeBufferToSeparateStrips (TIFF* out, uint8_t* buf,
80 stripsize = TIFFVStripSize(out, nrows);
81 src = buf + (row * rowsize);
82 - memset (obuf, '\0', rowstripsize);
83 + memset (obuf, '\0',rowstripsize + NUM_BUFF_OVERSIZE_BYTES);
84 if (extractContigSamplesToBuffer(obuf, src, nrows, width, s, spp, bps, dump))
87 @@ -1237,10 +1242,15 @@ writeBufferToSeparateStrips (TIFF* out, uint8_t* buf,
89 if ((dump->outfile != NULL) && (dump->level == 1))
91 - dump_info(dump->outfile, dump->format,"",
92 + if (scanlinesize > 0x0ffffffffULL) {
93 + dump_info(dump->infile, dump->format, "loadImage",
94 + "Attention: scanlinesize %"PRIu64" is larger than UINT32_MAX.\nFollowing dump might be wrong.",
97 + dump_info(dump->outfile, dump->format,"",
98 "Sample %2d, Strip: %2d, bytes: %4d, Row %4d, bytes: %4d, Input offset: %6d",
99 - s + 1, strip + 1, stripsize, row + 1, scanlinesize, src - buf);
100 - dump_buffer(dump->outfile, dump->format, nrows, scanlinesize, row, obuf);
101 + s + 1, strip + 1, stripsize, row + 1, (uint32_t)scanlinesize, src - buf);
102 + dump_buffer(dump->outfile, dump->format, nrows, (uint32_t)scanlinesize, row, obuf);
105 if (TIFFWriteEncodedStrip(out, strip++, obuf, stripsize) < 0)
106 @@ -1267,7 +1277,7 @@ static int writeBufferToContigTiles (TIFF* out, uint8_t* buf, uint32_t imageleng
108 uint32_t row, col, nrow, ncol;
109 uint32_t src_rowsize, col_offset;
110 - uint32_t tile_rowsize = TIFFTileRowSize(out);
111 + tmsize_t tile_rowsize = TIFFTileRowSize(out);
112 uint8_t* bufp = (uint8_t*) buf;
113 tsize_t tile_buffsize = 0;
114 tsize_t tilesize = TIFFTileSize(out);
115 @@ -1310,9 +1320,11 @@ static int writeBufferToContigTiles (TIFF* out, uint8_t* buf, uint32_t imageleng
117 src_rowsize = ((imagewidth * spp * bps) + 7U) / 8;
119 - tilebuf = limitMalloc(tile_buffsize);
120 + /* Add 3 padding bytes for extractContigSamples32bits */
121 + tilebuf = limitMalloc(tile_buffsize + NUM_BUFF_OVERSIZE_BYTES);
124 + memset(tilebuf, 0, tile_buffsize + NUM_BUFF_OVERSIZE_BYTES);
125 for (row = 0; row < imagelength; row += tl)
127 nrow = (row + tl > imagelength) ? imagelength - row : tl;
128 @@ -1358,7 +1370,8 @@ static int writeBufferToSeparateTiles (TIFF* out, uint8_t* buf, uint32_t imagele
129 uint32_t imagewidth, tsample_t spp,
130 struct dump_opts * dump)
132 - tdata_t obuf = limitMalloc(TIFFTileSize(out));
133 + /* Add 3 padding bytes for extractContigSamples32bits */
134 + tdata_t obuf = limitMalloc(TIFFTileSize(out) + NUM_BUFF_OVERSIZE_BYTES);
136 uint32_t row, col, nrow, ncol;
137 uint32_t src_rowsize, col_offset;
138 @@ -1368,6 +1381,7 @@ static int writeBufferToSeparateTiles (TIFF* out, uint8_t* buf, uint32_t imagele
142 + memset(obuf, 0, TIFFTileSize(out) + NUM_BUFF_OVERSIZE_BYTES);
144 if( !TIFFGetField(out, TIFFTAG_TILELENGTH, &tl) ||
145 !TIFFGetField(out, TIFFTAG_TILEWIDTH, &tw) ||
146 @@ -1793,14 +1807,14 @@ void process_command_opts (int argc, char *argv[], char *mp, char *mode, uint32
149 /* convert option to lowercase */
150 - end = strlen (opt_ptr);
151 + end = (unsigned int)strlen (opt_ptr);
152 for (i = 0; i < end; i++)
153 *(opt_ptr + i) = tolower((int) *(opt_ptr + i));
154 /* Look for dump format specification */
155 if (strncmp(opt_ptr, "for", 3) == 0)
157 /* convert value to lowercase */
158 - end = strlen (opt_offset + 1);
159 + end = (unsigned int)strlen (opt_offset + 1);
160 for (i = 1; i <= end; i++)
161 *(opt_offset + i) = tolower((int) *(opt_offset + i));
162 /* check dump format value */
163 @@ -2273,6 +2287,8 @@ main(int argc, char* argv[])
165 char temp_filename[PATH_MAX + 16]; /* Extra space keeps the compiler from complaining */
167 + assert(NUM_BUFF_OVERSIZE_BYTES >= 3);
169 little_endian = *((unsigned char *)&little_endian) & '1';
171 initImageData(&image);
172 @@ -3227,13 +3243,13 @@ extractContigSamples32bits (uint8_t *in, uint8_t *out, uint32_t cols,
173 /* If we have a full buffer's worth, write it out */
174 if (ready_bits >= 32)
176 - bytebuff1 = (buff2 >> 56);
177 + bytebuff1 = (uint8_t)(buff2 >> 56);
179 - bytebuff2 = (buff2 >> 48);
180 + bytebuff2 = (uint8_t)(buff2 >> 48);
182 - bytebuff3 = (buff2 >> 40);
183 + bytebuff3 = (uint8_t)(buff2 >> 40);
185 - bytebuff4 = (buff2 >> 32);
186 + bytebuff4 = (uint8_t)(buff2 >> 32);
190 @@ -3642,13 +3658,13 @@ extractContigSamplesShifted32bits (uint8_t *in, uint8_t *out, uint32_t cols,
192 else /* If we have a full buffer's worth, write it out */
194 - bytebuff1 = (buff2 >> 56);
195 + bytebuff1 = (uint8_t)(buff2 >> 56);
197 - bytebuff2 = (buff2 >> 48);
198 + bytebuff2 = (uint8_t)(buff2 >> 48);
200 - bytebuff3 = (buff2 >> 40);
201 + bytebuff3 = (uint8_t)(buff2 >> 40);
203 - bytebuff4 = (buff2 >> 32);
204 + bytebuff4 = (uint8_t)(buff2 >> 32);
208 @@ -3825,10 +3841,10 @@ extractContigSamplesToTileBuffer(uint8_t *out, uint8_t *in, uint32_t rows, uint3
209 static int readContigStripsIntoBuffer (TIFF* in, uint8_t* buf)
212 - int32_t bytes_read = 0;
213 + tmsize_t bytes_read = 0;
214 uint32_t strip, nstrips = TIFFNumberOfStrips(in);
215 - uint32_t stripsize = TIFFStripSize(in);
217 + tmsize_t stripsize = TIFFStripSize(in);
219 uint32_t rps = TIFFGetFieldDefaulted(in, TIFFTAG_ROWSPERSTRIP, &rps);
220 tsize_t scanline_size = TIFFScanlineSize(in);
222 @@ -3841,11 +3857,11 @@ static int readContigStripsIntoBuffer (TIFF* in, uint8_t* buf)
223 bytes_read = TIFFReadEncodedStrip (in, strip, bufp, -1);
224 rows = bytes_read / scanline_size;
225 if ((strip < (nstrips - 1)) && (bytes_read != (int32_t)stripsize))
226 - TIFFError("", "Strip %"PRIu32": read %"PRId32" bytes, strip size %"PRIu32,
227 + TIFFError("", "Strip %"PRIu32": read %"PRId64" bytes, strip size %"PRIu64,
228 strip + 1, bytes_read, stripsize);
230 if (bytes_read < 0 && !ignore) {
231 - TIFFError("", "Error reading strip %"PRIu32" after %"PRIu32" rows",
232 + TIFFError("", "Error reading strip %"PRIu32" after %"PRIu64" rows",
236 @@ -4310,13 +4326,13 @@ combineSeparateSamples32bits (uint8_t *in[], uint8_t *out, uint32_t cols,
237 /* If we have a full buffer's worth, write it out */
238 if (ready_bits >= 32)
240 - bytebuff1 = (buff2 >> 56);
241 + bytebuff1 = (uint8_t)(buff2 >> 56);
243 - bytebuff2 = (buff2 >> 48);
244 + bytebuff2 = (uint8_t)(buff2 >> 48);
246 - bytebuff3 = (buff2 >> 40);
247 + bytebuff3 = (uint8_t)(buff2 >> 40);
249 - bytebuff4 = (buff2 >> 32);
250 + bytebuff4 = (uint8_t)(buff2 >> 32);
254 @@ -4359,10 +4375,10 @@ combineSeparateSamples32bits (uint8_t *in[], uint8_t *out, uint32_t cols,
255 "Row %3d, Col %3d, Src byte offset %3d bit offset %2d Dst offset %3d",
256 row + 1, col + 1, src_byte, src_bit, dst - out);
258 - dump_long (dumpfile, format, "Match bits ", matchbits);
259 + dump_wide (dumpfile, format, "Match bits ", matchbits);
260 dump_data (dumpfile, format, "Src bits ", src, 4);
261 - dump_long (dumpfile, format, "Buff1 bits ", buff1);
262 - dump_long (dumpfile, format, "Buff2 bits ", buff2);
263 + dump_wide (dumpfile, format, "Buff1 bits ", buff1);
264 + dump_wide (dumpfile, format, "Buff2 bits ", buff2);
265 dump_byte (dumpfile, format, "Write bits1", bytebuff1);
266 dump_byte (dumpfile, format, "Write bits2", bytebuff2);
267 dump_info (dumpfile, format, "", "Ready bits: %2d", ready_bits);
268 @@ -4835,13 +4851,13 @@ combineSeparateTileSamples32bits (uint8_t *in[], uint8_t *out, uint32_t cols,
269 /* If we have a full buffer's worth, write it out */
270 if (ready_bits >= 32)
272 - bytebuff1 = (buff2 >> 56);
273 + bytebuff1 = (uint8_t)(buff2 >> 56);
275 - bytebuff2 = (buff2 >> 48);
276 + bytebuff2 = (uint8_t)(buff2 >> 48);
278 - bytebuff3 = (buff2 >> 40);
279 + bytebuff3 = (uint8_t)(buff2 >> 40);
281 - bytebuff4 = (buff2 >> 32);
282 + bytebuff4 = (uint8_t)(buff2 >> 32);
286 @@ -4884,10 +4900,10 @@ combineSeparateTileSamples32bits (uint8_t *in[], uint8_t *out, uint32_t cols,
287 "Row %3d, Col %3d, Src byte offset %3d bit offset %2d Dst offset %3d",
288 row + 1, col + 1, src_byte, src_bit, dst - out);
290 - dump_long (dumpfile, format, "Match bits ", matchbits);
291 + dump_wide (dumpfile, format, "Match bits ", matchbits);
292 dump_data (dumpfile, format, "Src bits ", src, 4);
293 - dump_long (dumpfile, format, "Buff1 bits ", buff1);
294 - dump_long (dumpfile, format, "Buff2 bits ", buff2);
295 + dump_wide (dumpfile, format, "Buff1 bits ", buff1);
296 + dump_wide (dumpfile, format, "Buff2 bits ", buff2);
297 dump_byte (dumpfile, format, "Write bits1", bytebuff1);
298 dump_byte (dumpfile, format, "Write bits2", bytebuff2);
299 dump_info (dumpfile, format, "", "Ready bits: %2d", ready_bits);
300 @@ -4910,7 +4926,7 @@ static int readSeparateStripsIntoBuffer (TIFF *in, uint8_t *obuf, uint32_t lengt
302 int i, bytes_per_sample, bytes_per_pixel, shift_width, result = 1;
304 - int32_t bytes_read = 0;
305 + tmsize_t bytes_read = 0;
306 uint16_t bps = 0, planar;
308 uint32_t strips_per_sample;
309 @@ -4976,7 +4992,7 @@ static int readSeparateStripsIntoBuffer (TIFF *in, uint8_t *obuf, uint32_t lengt
310 for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++)
313 - buff = limitMalloc(stripsize + 3);
314 + buff = limitMalloc(stripsize + NUM_BUFF_OVERSIZE_BYTES);
317 TIFFError ("readSeparateStripsIntoBuffer",
318 @@ -4999,7 +5015,7 @@ static int readSeparateStripsIntoBuffer (TIFF *in, uint8_t *obuf, uint32_t lengt
320 strip = (s * strips_per_sample) + j;
321 bytes_read = TIFFReadEncodedStrip (in, strip, buff, stripsize);
322 - rows_this_strip = bytes_read / src_rowsize;
323 + rows_this_strip = (uint32_t)(bytes_read / src_rowsize);
324 if (bytes_read < 0 && !ignore)
326 TIFFError(TIFFFileName(in),
327 @@ -6062,13 +6078,14 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
328 uint16_t input_compression = 0, input_photometric = 0;
329 uint16_t subsampling_horiz, subsampling_vert;
330 uint32_t width = 0, length = 0;
331 - uint32_t stsize = 0, tlsize = 0, buffsize = 0, scanlinesize = 0;
332 + tmsize_t stsize = 0, tlsize = 0, buffsize = 0;
333 + tmsize_t scanlinesize = 0;
334 uint32_t tw = 0, tl = 0; /* Tile width and length */
335 - uint32_t tile_rowsize = 0;
336 + tmsize_t tile_rowsize = 0;
337 unsigned char *read_buff = NULL;
338 unsigned char *new_buff = NULL;
340 - static uint32_t prev_readsize = 0;
341 + static tmsize_t prev_readsize = 0;
343 TIFFGetFieldDefaulted(in, TIFFTAG_BITSPERSAMPLE, &bps);
344 TIFFGetFieldDefaulted(in, TIFFTAG_SAMPLESPERPIXEL, &spp);
345 @@ -6325,6 +6342,8 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
346 /* The buffsize_check and the possible adaptation of buffsize
347 * has to account also for padding of each line to a byte boundary.
348 * This is assumed by mirrorImage() and rotateImage().
349 + * Furthermore, functions like extractContigSamplesShifted32bits()
350 + * need a buffer, which is at least 3 bytes larger than the actual image.
351 * Otherwise buffer-overflow might occur there.
353 buffsize_check = length * (uint32_t)(((width * spp * bps) + 7) / 8);
354 @@ -6376,7 +6395,7 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
355 TIFFError("loadImage", "Unable to allocate/reallocate read buffer");
358 - read_buff = (unsigned char *)limitMalloc(buffsize+3);
359 + read_buff = (unsigned char *)limitMalloc(buffsize + NUM_BUFF_OVERSIZE_BYTES);
363 @@ -6387,11 +6406,11 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
364 TIFFError("loadImage", "Unable to allocate/reallocate read buffer");
367 - new_buff = _TIFFrealloc(read_buff, buffsize+3);
368 + new_buff = _TIFFrealloc(read_buff, buffsize + NUM_BUFF_OVERSIZE_BYTES);
372 - read_buff = (unsigned char *)limitMalloc(buffsize+3);
373 + read_buff = (unsigned char *)limitMalloc(buffsize + NUM_BUFF_OVERSIZE_BYTES);
376 read_buff = new_buff;
377 @@ -6464,8 +6483,13 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
378 dump_info (dump->infile, dump->format, "",
379 "Bits per sample %"PRIu16", Samples per pixel %"PRIu16, bps, spp);
381 + if (scanlinesize > 0x0ffffffffULL) {
382 + dump_info(dump->infile, dump->format, "loadImage",
383 + "Attention: scanlinesize %"PRIu64" is larger than UINT32_MAX.\nFollowing dump might be wrong.",
386 for (i = 0; i < length; i++)
387 - dump_buffer(dump->infile, dump->format, 1, scanlinesize,
388 + dump_buffer(dump->infile, dump->format, 1, (uint32_t)scanlinesize,
389 i, read_buff + (i * scanlinesize));
392 @@ -7485,13 +7509,13 @@ writeSingleSection(TIFF *in, TIFF *out, struct image_data *image,
393 if (TIFFGetField(in, TIFFTAG_NUMBEROFINKS, &ninks)) {
394 TIFFSetField(out, TIFFTAG_NUMBEROFINKS, ninks);
395 if (TIFFGetField(in, TIFFTAG_INKNAMES, &inknames)) {
396 - int inknameslen = strlen(inknames) + 1;
397 + int inknameslen = (int)strlen(inknames) + 1;
398 const char* cp = inknames;
400 cp = strchr(cp, '\0');
403 - inknameslen += (strlen(cp) + 1);
404 + inknameslen += ((int)strlen(cp) + 1);
408 @@ -7554,23 +7578,23 @@ createImageSection(uint32_t sectsize, unsigned char **sect_buff_ptr)
412 - sect_buff = (unsigned char *)limitMalloc(sectsize);
413 + sect_buff = (unsigned char *)limitMalloc(sectsize + NUM_BUFF_OVERSIZE_BYTES);
416 TIFFError("createImageSection", "Unable to allocate/reallocate section buffer");
419 - _TIFFmemset(sect_buff, 0, sectsize);
420 + _TIFFmemset(sect_buff, 0, sectsize + NUM_BUFF_OVERSIZE_BYTES);
424 if (prev_sectsize < sectsize)
426 - new_buff = _TIFFrealloc(sect_buff, sectsize);
427 + new_buff = _TIFFrealloc(sect_buff, sectsize + NUM_BUFF_OVERSIZE_BYTES);
430 _TIFFfree (sect_buff);
431 - sect_buff = (unsigned char *)limitMalloc(sectsize);
432 + sect_buff = (unsigned char *)limitMalloc(sectsize + NUM_BUFF_OVERSIZE_BYTES);
435 sect_buff = new_buff;
436 @@ -7580,7 +7604,7 @@ createImageSection(uint32_t sectsize, unsigned char **sect_buff_ptr)
437 TIFFError("createImageSection", "Unable to allocate/reallocate section buffer");
440 - _TIFFmemset(sect_buff, 0, sectsize);
441 + _TIFFmemset(sect_buff, 0, sectsize + NUM_BUFF_OVERSIZE_BYTES);
445 @@ -7611,17 +7635,17 @@ processCropSelections(struct image_data *image, struct crop_mask *crop,
446 cropsize = crop->bufftotal;
447 crop_buff = seg_buffs[0].buffer;
449 - crop_buff = (unsigned char *)limitMalloc(cropsize);
450 + crop_buff = (unsigned char *)limitMalloc(cropsize + NUM_BUFF_OVERSIZE_BYTES);
453 prev_cropsize = seg_buffs[0].size;
454 if (prev_cropsize < cropsize)
456 - next_buff = _TIFFrealloc(crop_buff, cropsize);
457 + next_buff = _TIFFrealloc(crop_buff, cropsize + NUM_BUFF_OVERSIZE_BYTES);
460 _TIFFfree (crop_buff);
461 - crop_buff = (unsigned char *)limitMalloc(cropsize);
462 + crop_buff = (unsigned char *)limitMalloc(cropsize + NUM_BUFF_OVERSIZE_BYTES);
465 crop_buff = next_buff;
466 @@ -7634,7 +7658,7 @@ processCropSelections(struct image_data *image, struct crop_mask *crop,
470 - _TIFFmemset(crop_buff, 0, cropsize);
471 + _TIFFmemset(crop_buff, 0, cropsize + NUM_BUFF_OVERSIZE_BYTES);
472 seg_buffs[0].buffer = crop_buff;
473 seg_buffs[0].size = cropsize;
475 @@ -7714,17 +7738,17 @@ processCropSelections(struct image_data *image, struct crop_mask *crop,
476 cropsize = crop->bufftotal;
477 crop_buff = seg_buffs[i].buffer;
479 - crop_buff = (unsigned char *)limitMalloc(cropsize);
480 + crop_buff = (unsigned char *)limitMalloc(cropsize + NUM_BUFF_OVERSIZE_BYTES);
483 prev_cropsize = seg_buffs[0].size;
484 if (prev_cropsize < cropsize)
486 - next_buff = _TIFFrealloc(crop_buff, cropsize);
487 + next_buff = _TIFFrealloc(crop_buff, cropsize + NUM_BUFF_OVERSIZE_BYTES);
490 _TIFFfree (crop_buff);
491 - crop_buff = (unsigned char *)limitMalloc(cropsize);
492 + crop_buff = (unsigned char *)limitMalloc(cropsize + NUM_BUFF_OVERSIZE_BYTES);
495 crop_buff = next_buff;
496 @@ -7737,7 +7761,7 @@ processCropSelections(struct image_data *image, struct crop_mask *crop,
500 - _TIFFmemset(crop_buff, 0, cropsize);
501 + _TIFFmemset(crop_buff, 0, cropsize + NUM_BUFF_OVERSIZE_BYTES);
502 seg_buffs[i].buffer = crop_buff;
503 seg_buffs[i].size = cropsize;
505 @@ -7853,24 +7877,24 @@ createCroppedImage(struct image_data *image, struct crop_mask *crop,
506 crop_buff = *crop_buff_ptr;
509 - crop_buff = (unsigned char *)limitMalloc(cropsize);
510 + crop_buff = (unsigned char *)limitMalloc(cropsize + NUM_BUFF_OVERSIZE_BYTES);
513 TIFFError("createCroppedImage", "Unable to allocate/reallocate crop buffer");
516 - _TIFFmemset(crop_buff, 0, cropsize);
517 + _TIFFmemset(crop_buff, 0, cropsize + NUM_BUFF_OVERSIZE_BYTES);
518 prev_cropsize = cropsize;
522 if (prev_cropsize < cropsize)
524 - new_buff = _TIFFrealloc(crop_buff, cropsize);
525 + new_buff = _TIFFrealloc(crop_buff, cropsize + NUM_BUFF_OVERSIZE_BYTES);
529 - crop_buff = (unsigned char *)limitMalloc(cropsize);
530 + crop_buff = (unsigned char *)limitMalloc(cropsize + NUM_BUFF_OVERSIZE_BYTES);
533 crop_buff = new_buff;
534 @@ -7879,7 +7903,7 @@ createCroppedImage(struct image_data *image, struct crop_mask *crop,
535 TIFFError("createCroppedImage", "Unable to allocate/reallocate crop buffer");
538 - _TIFFmemset(crop_buff, 0, cropsize);
539 + _TIFFmemset(crop_buff, 0, cropsize + NUM_BUFF_OVERSIZE_BYTES);
543 @@ -8177,13 +8201,13 @@ writeCroppedImage(TIFF *in, TIFF *out, struct image_data *image,
544 if (TIFFGetField(in, TIFFTAG_NUMBEROFINKS, &ninks)) {
545 TIFFSetField(out, TIFFTAG_NUMBEROFINKS, ninks);
546 if (TIFFGetField(in, TIFFTAG_INKNAMES, &inknames)) {
547 - int inknameslen = strlen(inknames) + 1;
548 + int inknameslen = (int)strlen(inknames) + 1;
549 const char* cp = inknames;
551 cp = strchr(cp, '\0');
554 - inknameslen += (strlen(cp) + 1);
555 + inknameslen += ((int)strlen(cp) + 1);
559 @@ -8568,13 +8592,13 @@ rotateContigSamples32bits(uint16_t rotation, uint16_t spp, uint16_t bps, uint32_
561 else /* If we have a full buffer's worth, write it out */
563 - bytebuff1 = (buff2 >> 56);
564 + bytebuff1 = (uint8_t)(buff2 >> 56);
566 - bytebuff2 = (buff2 >> 48);
567 + bytebuff2 = (uint8_t)(buff2 >> 48);
569 - bytebuff3 = (buff2 >> 40);
570 + bytebuff3 = (uint8_t)(buff2 >> 40);
572 - bytebuff4 = (buff2 >> 32);
573 + bytebuff4 = (uint8_t)(buff2 >> 32);
577 @@ -8643,12 +8667,13 @@ rotateImage(uint16_t rotation, struct image_data *image, uint32_t *img_width,
581 - if (!(rbuff = (unsigned char *)limitMalloc(buffsize)))
582 + /* Add 3 padding bytes for extractContigSamplesShifted32bits */
583 + if (!(rbuff = (unsigned char *)limitMalloc(buffsize + NUM_BUFF_OVERSIZE_BYTES)))
585 - TIFFError("rotateImage", "Unable to allocate rotation buffer of %1u bytes", buffsize);
586 + TIFFError("rotateImage", "Unable to allocate rotation buffer of %1u bytes", buffsize + NUM_BUFF_OVERSIZE_BYTES);
589 - _TIFFmemset(rbuff, '\0', buffsize);
590 + _TIFFmemset(rbuff, '\0', buffsize + NUM_BUFF_OVERSIZE_BYTES);
594 @@ -9176,13 +9201,13 @@ reverseSamples32bits (uint16_t spp, uint16_t bps, uint32_t width,
596 else /* If we have a full buffer's worth, write it out */
598 - bytebuff1 = (buff2 >> 56);
599 + bytebuff1 = (uint8_t)(buff2 >> 56);
601 - bytebuff2 = (buff2 >> 48);
602 + bytebuff2 = (uint8_t)(buff2 >> 48);
604 - bytebuff3 = (buff2 >> 40);
605 + bytebuff3 = (uint8_t)(buff2 >> 40);
607 - bytebuff4 = (buff2 >> 32);
608 + bytebuff4 = (uint8_t)(buff2 >> 32);
612 @@ -9273,12 +9298,13 @@ mirrorImage(uint16_t spp, uint16_t bps, uint16_t mirror, uint32_t width, uint32_
616 - line_buff = (unsigned char *)limitMalloc(rowsize);
617 + line_buff = (unsigned char *)limitMalloc(rowsize + NUM_BUFF_OVERSIZE_BYTES);
618 if (line_buff == NULL)
620 - TIFFError ("mirrorImage", "Unable to allocate mirror line buffer of %1u bytes", rowsize);
621 + TIFFError ("mirrorImage", "Unable to allocate mirror line buffer of %1u bytes", rowsize + NUM_BUFF_OVERSIZE_BYTES);
624 + _TIFFmemset(line_buff, '\0', rowsize + NUM_BUFF_OVERSIZE_BYTES);
626 dst = ibuff + (rowsize * (length - 1));
627 for (row = 0; row < length / 2; row++)
628 @@ -9310,11 +9336,12 @@ mirrorImage(uint16_t spp, uint16_t bps, uint16_t mirror, uint32_t width, uint32_
631 { /* non 8 bit per sample data */
632 - if (!(line_buff = (unsigned char *)limitMalloc(rowsize + 1)))
633 + if (!(line_buff = (unsigned char *)limitMalloc(rowsize + NUM_BUFF_OVERSIZE_BYTES)))
635 TIFFError("mirrorImage", "Unable to allocate mirror line buffer");
638 + _TIFFmemset(line_buff, '\0', rowsize + NUM_BUFF_OVERSIZE_BYTES);
639 bytes_per_sample = (bps + 7) / 8;
640 bytes_per_pixel = ((bps * spp) + 7) / 8;
641 if (bytes_per_pixel < (bytes_per_sample + 1))
642 @@ -9326,7 +9353,7 @@ mirrorImage(uint16_t spp, uint16_t bps, uint16_t mirror, uint32_t width, uint32_
644 row_offset = row * rowsize;
645 src = ibuff + row_offset;
646 - _TIFFmemset (line_buff, '\0', rowsize);
647 + _TIFFmemset (line_buff, '\0', rowsize + NUM_BUFF_OVERSIZE_BYTES);
650 case 1: if (reverseSamples16bits(spp, bps, width, src, line_buff))