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

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Fri Nov 27 03:02:36 EET 2020


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 at gmail.com>
---
 libavcodec/smvjpegdec.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)

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);
 
-- 
2.25.1



More information about the ffmpeg-devel mailing list