diff mbox series

[FFmpeg-devel,10/21] avcodec/mscc: Use ff_inflate_init/end()

Message ID AS1PR01MB9564831B26B7B578DF1E8B908F109@AS1PR01MB9564.eurprd01.prod.exchangelabs.com
State Accepted
Commit 7fffa718c35d32aa99659524d74f6338247934b6
Headers show
Series [FFmpeg-devel,01/21] avcodec/pngenc: Avoid potentially truncating integers | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt March 15, 2022, 8:06 p.m. UTC
This fixes the problem of potentially closing a z_stream
that has never been successfully initialized.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure         |  4 ++--
 libavcodec/mscc.c | 33 +++++++++++++--------------------
 2 files changed, 15 insertions(+), 22 deletions(-)
diff mbox series

Patch

diff --git a/configure b/configure
index 46b02275ad..c79908c1bd 100755
--- a/configure
+++ b/configure
@@ -2883,7 +2883,7 @@  mpeg2video_encoder_select="mpegvideoenc h263dsp"
 mpeg4_decoder_select="h263_decoder mpeg4video_parser"
 mpeg4_encoder_select="h263_encoder"
 msa1_decoder_select="mss34dsp"
-mscc_decoder_deps="zlib"
+mscc_decoder_select="inflate_wrapper"
 msmpeg4v1_decoder_select="h263_decoder"
 msmpeg4v2_decoder_select="h263_decoder"
 msmpeg4v2_encoder_select="h263_encoder"
@@ -2935,7 +2935,7 @@  sonic_ls_encoder_select="golomb rangecoder"
 sp5x_decoder_select="mjpeg_decoder"
 speedhq_decoder_select="mpegvideo"
 speedhq_encoder_select="mpegvideoenc"
-srgc_decoder_deps="zlib"
+srgc_decoder_select="inflate_wrapper"
 svq1_decoder_select="hpeldsp"
 svq1_encoder_select="hpeldsp me_cmp mpegvideoenc"
 svq3_decoder_select="golomb h264dsp h264parse h264pred hpeldsp tpeldsp videodsp"
diff --git a/libavcodec/mscc.c b/libavcodec/mscc.c
index d9a6de094a..19cfce796e 100644
--- a/libavcodec/mscc.c
+++ b/libavcodec/mscc.c
@@ -27,6 +27,7 @@ 
 #include "avcodec.h"
 #include "bytestream.h"
 #include "internal.h"
+#include "zlib_wrapper.h"
 
 #include <zlib.h>
 
@@ -36,7 +37,7 @@  typedef struct MSCCContext {
     uint8_t          *decomp_buf;
     unsigned int      uncomp_size;
     uint8_t          *uncomp_buf;
-    z_stream          zstream;
+    FFZStream         zstream;
 
     uint32_t          pal[256];
 } MSCCContext;
@@ -132,6 +133,7 @@  static int decode_frame(AVCodecContext *avctx,
                         AVPacket *avpkt)
 {
     MSCCContext *s = avctx->priv_data;
+    z_stream *const zstream = &s->zstream.zstream;
     AVFrame *frame = data;
     uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
@@ -166,22 +168,22 @@  static int decode_frame(AVCodecContext *avctx,
         memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
     }
 
-    ret = inflateReset(&s->zstream);
+    ret = inflateReset(zstream);
     if (ret != Z_OK) {
         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
         return AVERROR_UNKNOWN;
     }
-    s->zstream.next_in   = buf;
-    s->zstream.avail_in  = buf_size;
-    s->zstream.next_out  = s->decomp_buf;
-    s->zstream.avail_out = s->decomp_size;
-    ret = inflate(&s->zstream, Z_FINISH);
+    zstream->next_in   = buf;
+    zstream->avail_in  = buf_size;
+    zstream->next_out  = s->decomp_buf;
+    zstream->avail_out = s->decomp_size;
+    ret = inflate(zstream, Z_FINISH);
     if (ret != Z_STREAM_END) {
         av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
         return AVERROR_UNKNOWN;
     }
 
-    bytestream2_init(&gb, s->decomp_buf, s->zstream.total_out);
+    bytestream2_init(&gb, s->decomp_buf, zstream->total_out);
     bytestream2_init_writer(&pb, s->uncomp_buf, s->uncomp_size);
 
     ret = rle_uncompress(avctx, &gb, &pb);
@@ -204,7 +206,7 @@  static int decode_frame(AVCodecContext *avctx,
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     MSCCContext *s = avctx->priv_data;
-    int stride, zret;
+    int stride;
 
     switch (avctx->bits_per_coded_sample) {
     case  8: avctx->pix_fmt = AV_PIX_FMT_PAL8;   break;
@@ -227,16 +229,7 @@  static av_cold int decode_init(AVCodecContext *avctx)
     if (!(s->uncomp_buf = av_malloc(s->uncomp_size)))
         return AVERROR(ENOMEM);
 
-    s->zstream.zalloc = Z_NULL;
-    s->zstream.zfree = Z_NULL;
-    s->zstream.opaque = Z_NULL;
-    zret = inflateInit(&s->zstream);
-    if (zret != Z_OK) {
-        av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
-        return AVERROR_UNKNOWN;
-    }
-
-    return 0;
+    return ff_inflate_init(&s->zstream, avctx);
 }
 
 static av_cold int decode_close(AVCodecContext *avctx)
@@ -247,7 +240,7 @@  static av_cold int decode_close(AVCodecContext *avctx)
     s->decomp_size = 0;
     av_freep(&s->uncomp_buf);
     s->uncomp_size = 0;
-    inflateEnd(&s->zstream);
+    ff_inflate_end(&s->zstream);
 
     return 0;
 }