From 33d44e86fb649cbd3db23a7cfe7b81464fc74109 Mon Sep 17 00:00:00 2001 From: Ruven Date: Thu, 22 Sep 2022 14:36:27 +0200 Subject: [PATCH] SubIFD offsets should only be added if smaller pyramid layers actually exist. Otherwise results in corrupted TIFF files for images smaller than the tile size (#3058) --- libvips/foreign/vips2tiff.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libvips/foreign/vips2tiff.c b/libvips/foreign/vips2tiff.c index a53ac4f7..e720bdb6 100644 --- a/libvips/foreign/vips2tiff.c +++ b/libvips/foreign/vips2tiff.c @@ -2127,11 +2127,14 @@ wtiff_page_start( Wtiff *wtiff ) */ for( n_layers = 0, p = wtiff->layer->below; p; p = p->below ) n_layers += 1; - subifd_offsets = VIPS_ARRAY( NULL, n_layers, toff_t ); - memset( subifd_offsets, 0, n_layers * sizeof( toff_t ) ); - TIFFSetField( wtiff->layer->tif, TIFFTAG_SUBIFD, - n_layers, subifd_offsets ); - g_free( subifd_offsets ); + + if( n_layers > 0 ){ + subifd_offsets = VIPS_ARRAY( NULL, n_layers, toff_t ); + memset( subifd_offsets, 0, n_layers * sizeof( toff_t ) ); + TIFFSetField( wtiff->layer->tif, TIFFTAG_SUBIFD, + n_layers, subifd_offsets ); + g_free( subifd_offsets ); + } } return( 0 );