Mercurial > octave-libtiff
changeset 31110:2daeeff33980
Tiff read fixed segfault bug for compressed images
* __tiff__.cc (F__tiff_read__): fixed bug for wrong bufer size
for compressed images, clean up memory.
author | magedrifaat <magedrifaat@gmail.com> |
---|---|
date | Thu, 07 Jul 2022 01:54:20 +0200 |
parents | 06814e8b5a29 |
children | 5d79d99c96b9 |
files | libinterp/dldfcn/__tiff__.cc scripts/io/Tiff.m |
diffstat | 2 files changed, 19 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/dldfcn/__tiff__.cc Wed Jul 06 04:44:17 2022 +0200 +++ b/libinterp/dldfcn/__tiff__.cc Thu Jul 07 01:54:20 2022 +0200 @@ -590,7 +590,7 @@ if (! TIFFGetField (tif, TIFFTAG_PLANARCONFIG, &planar_configuration)) error ("Failed to read the PlanarConfiguration tag"); - uint16_t is_tiled = TIFFIsTiled(tif); + int is_tiled = TIFFIsTiled(tif); // Create memory for storing the image data // TODO(maged): replace malloc with a suitable C++ structure @@ -611,24 +611,20 @@ { // Obtain the necessary data for handling the strips uint32_t strip_count = TIFFNumberOfStrips (tif); - uint32_t *strip_byte_counts; - if (! TIFFGetField (tif, TIFFTAG_STRIPBYTECOUNTS, &strip_byte_counts)) - error ("Failed to read StripByteCounts tag"); - - uint32_t strip_size = strip_byte_counts[0]; - tdata_t buf = _TIFFmalloc (strip_size); + tdata_t buf = _TIFFmalloc (TIFFStripSize (tif)); + if (! buf) + error ("Failed to allocate buffer for strip data"); + uint32_t row_index = 0; for (uint32_t strip = 0; strip < strip_count; strip++) { - if (strip_size < strip_byte_counts[strip]) - { - buf = _TIFFrealloc (buf, strip_byte_counts[strip]); - strip_size = strip_byte_counts[strip]; - } - TIFFReadEncodedStrip (tif, strip, buf, -1); + uint32_t strip_bytes; + if ((strip_bytes = TIFFReadEncodedStrip (tif, strip, buf, -1)) == -1) + error ("Failed to read strip data"); + if (planar_configuration == PLANARCONFIG_CONTIG) { - uint32_t rows_in_strip = strip_byte_counts[strip] / row_size; + uint32_t rows_in_strip = strip_bytes / row_size; for (uint32_t row_subindex = 0; row_subindex < rows_in_strip; row_subindex++) @@ -712,6 +708,14 @@ default: error ("Unsupported bit depth"); } + + for (uint32_t row = 0; row < height; row++) + { + for (uint32_t column = 0; column < width; column++) + free (image[row][column]); + + free (image[row]); + } return retval; #else err_disabled_feature ("read", "Tiff");
--- a/scripts/io/Tiff.m Wed Jul 06 04:44:17 2022 +0200 +++ b/scripts/io/Tiff.m Thu Jul 07 01:54:20 2022 +0200 @@ -97,7 +97,7 @@ endfunction function argout = read(t) - argout = __tiff_read__(t.tiff_handle) + argout = __tiff_read__(t.tiff_handle); endfunction % TODO(maged): add documentation and make print_usage work