diff --git a/src/dec/webp.c b/src/dec/webp.c index c9772cd0..823eafe6 100644 --- a/src/dec/webp.c +++ b/src/dec/webp.c @@ -311,7 +311,7 @@ void WebPInitCustomIo(VP8Io* const io) { } //----------------------------------------------------------------------------- -// Init/Check decoding parameters +// Init/Check/Free decoding parameters and buffer int WebPInitDecParams(const uint8_t* data, uint32_t data_size, int* width, int* height, WebPDecParams* const params) { @@ -376,6 +376,11 @@ int WebPCheckDecParams(const VP8Io* io, const WebPDecParams* params, return ok; } +void WebPClearDecParams(WebPDecParams* params) { + free(params->output); + memset(params, 0, sizeof(*params)); +} + //----------------------------------------------------------------------------- // "Into" variants @@ -510,11 +515,14 @@ static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* data, if (!WebPInitDecParams(data, data_size, width, height, ¶ms)) { return NULL; } - if (params_out) *params_out = params; size = params.stride * (*height); uv_size = params.u_stride * ((*height + 1) / 2); - return DecodeInto(mode, data, data_size, ¶ms, size, uv_size, uv_size); + if (!DecodeInto(mode, data, data_size, ¶ms, size, uv_size, uv_size)) { + WebPClearDecParams(¶ms); + } + if (params_out) *params_out = params; + return params.output; } uint8_t* WebPDecodeRGB(const uint8_t* data, uint32_t data_size, diff --git a/src/dec/webpi.h b/src/dec/webpi.h index edb4887d..9d53de47 100644 --- a/src/dec/webpi.h +++ b/src/dec/webpi.h @@ -52,6 +52,10 @@ int WebPInitDecParams(const uint8_t* data, uint32_t data_size, int* width, int WebPCheckDecParams(const VP8Io* io, const WebPDecParams* params, int output_size, int output_u_size, int output_v_size); +// Deallocate memory allocated by WebPInitDecParams() and reset the +// WebPDecParams object. +void WebPClearDecParams(WebPDecParams* params); + #if defined(__cplusplus) || defined(c_plusplus) } // extern "C" #endif