diff mbox series

[FFmpeg-devel,14/23] avcodec/tscc: Don't free uninitialized z_stream

Message ID VI1PR0301MB2159883DD9D79C00B2C21DC88F589@VI1PR0301MB2159.eurprd03.prod.outlook.com
State Accepted
Commit 54cb70594da4b9a35885053fd8738040f447003d
Headers show
Series [FFmpeg-devel,01/23] avcodec/ljpegenc: Mark encoder as init-threadsafe | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt May 6, 2021, 5:11 a.m. UTC
It is not documented to be safe to call inflateEnd() on a z_stream
that has not been successfully initialized via inflateInit(); so
record whether it has been successfully initialized.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/tscc.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/tscc.c b/libavcodec/tscc.c
index 043d78d018..e1a075faa7 100644
--- a/libavcodec/tscc.c
+++ b/libavcodec/tscc.c
@@ -57,6 +57,7 @@  typedef struct TsccContext {
     unsigned char* decomp_buf;
     GetByteContext gb;
     int height;
+    int zlib_init_ok;
     z_stream zstream;
 
     uint32_t pal[256];
@@ -128,8 +129,6 @@  static av_cold int decode_init(AVCodecContext *avctx)
 
     c->height = avctx->height;
 
-    // Needed if zlib unused or init aborted before inflateInit
-    memset(&c->zstream, 0, sizeof(z_stream));
     switch(avctx->bits_per_coded_sample){
     case  8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
     case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
@@ -160,6 +159,7 @@  static av_cold int decode_init(AVCodecContext *avctx)
         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
         return AVERROR_UNKNOWN;
     }
+    c->zlib_init_ok = 1;
 
     c->frame = av_frame_alloc();
     if (!c->frame)
@@ -175,7 +175,8 @@  static av_cold int decode_end(AVCodecContext *avctx)
     av_freep(&c->decomp_buf);
     av_frame_free(&c->frame);
 
-    inflateEnd(&c->zstream);
+    if (c->zlib_init_ok)
+        inflateEnd(&c->zstream);
 
     return 0;
 }