From e1a9397c15c2dbf342436d7e9672083f06f31c79 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 5 Jan 2021 03:54:48 +0000 Subject: [PATCH] better error detection in spngload Do a speculative spng_get_trns() in header read to force all chunks to be parsed. Thanks randy408 See https://github.com/randy408/libspng/issues/145#issuecomment-744495084 --- ChangeLog | 1 + libvips/foreign/spngload.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/ChangeLog b/ChangeLog index a6a90323..a23f7114 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,7 @@ - check for null memory sources [kleisauke] - revise ppmload, fixing a couple of small bugs - signal error on EOF in jpegload more reliably [bozaro] +- better error detection in spngload [randy408] 18/12/20 started 8.10.5 - fix potential /0 in animated webp load [lovell] diff --git a/libvips/foreign/spngload.c b/libvips/foreign/spngload.c index b3e6a601..83340f6b 100644 --- a/libvips/foreign/spngload.c +++ b/libvips/foreign/spngload.c @@ -373,11 +373,25 @@ vips_foreign_load_png_header( VipsForeignLoad *load ) png->ihdr.bit_depth < 8 ) png->fmt = SPNG_FMT_G8; + /* Try reading the optional transparency chunk. This will cause all + * chunks up to the first IDAT to be read in, so it can fail if any + * chunk has an error. + */ + error = spng_get_trns( png->ctx, &trns ); + if( error && + error != SPNG_ECHUNKAVAIL ) { + vips_error( class->nickname, "%s", spng_strerror( error ) ); + return( -1 ); + } + /* Expand transparency. * * The _ALPHA types should not have the optional trns chunk (they * always have a transparent band), see * https://www.w3.org/TR/2003/REC-PNG-20031110/#11tRNS + * + * It's quick and safe to call spng_get_trns() again, and we now know + * it will only fail for no transparency chunk. */ if( png->ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA || png->ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR_ALPHA )