diff mbox series

[FFmpeg-devel,32/45] avcodec/smvjpegdec: Error out early if extradata is invalid

Message ID 20201127010249.2724610-32-andreas.rheinhardt@gmail.com
State New
Headers show
Series [FFmpeg-devel,01/45] avcodec/a64multienc: Fix memleak upon init failure
Related show

Checks

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

Commit Message

Andreas Rheinhardt Nov. 27, 2020, 1:02 a.m. UTC
Don't allocate and open a whole decoder after having already found out
that the given extradata is invalid.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/smvjpegdec.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/smvjpegdec.c b/libavcodec/smvjpegdec.c
index 973a9117f2..587ad82a00 100644
--- a/libavcodec/smvjpegdec.c
+++ b/libavcodec/smvjpegdec.c
@@ -92,9 +92,13 @@  static av_cold int smvjpeg_decode_init(AVCodecContext *avctx)
     SMVJpegDecodeContext *s = avctx->priv_data;
     AVCodec *codec;
     AVDictionary *thread_opt = NULL;
-    int ret = 0, r;
+    int ret;
 
-    s->frames_per_jpeg = 0;
+    if (avctx->extradata_size < 4 ||
+        (s->frames_per_jpeg = AV_RL32(avctx->extradata)) <= 0) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid number of frames per jpeg.\n");
+        return AVERROR_INVALIDDATA;
+    }
 
     s->picture[0] = av_frame_alloc();
     if (!s->picture[0])
@@ -108,14 +112,6 @@  static av_cold int smvjpeg_decode_init(AVCodecContext *avctx)
 
     s->jpg.picture_ptr      = s->picture[0];
 
-    if (avctx->extradata_size >= 4)
-        s->frames_per_jpeg = AV_RL32(avctx->extradata);
-
-    if (s->frames_per_jpeg <= 0) {
-        av_log(avctx, AV_LOG_ERROR, "Invalid number of frames per jpeg.\n");
-        ret = AVERROR_INVALIDDATA;
-    }
-
     codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
     if (!codec) {
         av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n");
@@ -129,9 +125,8 @@  static av_cold int smvjpeg_decode_init(AVCodecContext *avctx)
     s->avctx->refcounted_frames = 1;
     s->avctx->flags = avctx->flags;
     s->avctx->idct_algo = avctx->idct_algo;
-    if ((r = ff_codec_open2_recursive(s->avctx, codec, &thread_opt)) < 0) {
+    if ((ret = ff_codec_open2_recursive(s->avctx, codec, &thread_opt)) < 0) {
         av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n");
-        ret = r;
     }
     av_dict_free(&thread_opt);