avformat: Free the internal codec context at the end

Message ID 20170413120757.8C22D5DCDA@aruru.libav.org
State New
Headers show

Commit Message

Janne Grunau April 13, 2017, 12:07 p.m.
Module: libav
Branch: master
Commit: 9e4a5eb51b9f3b2bff0ef08e0074b7fe4893075d

Author:    Luca Barbato <lu_zero@gentoo.org>
Committer: Luca Barbato <lu_zero@gentoo.org>
Date:      Wed Apr 12 01:46:30 2017 +0200

avformat: Free the internal codec context at the end

Avoid a use after free in avformat_find_stream_info.

CC: libav-stable@libav.org

---

 libavformat/utils.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

Patch

diff --git a/libavformat/utils.c b/libavformat/utils.c
index 8fa89eb..eaba473 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -2466,11 +2466,6 @@  FF_ENABLE_DEPRECATION_WARNINGS
         count++;
     }
 
-    // close codecs which were opened in try_decode_frame()
-    for (i = 0; i < ic->nb_streams; i++) {
-        st = ic->streams[i];
-        avcodec_close(st->internal->avctx);
-    }
     for (i = 0; i < ic->nb_streams; i++) {
         st = ic->streams[i];
         avctx = st->internal->avctx;
@@ -2570,6 +2565,7 @@  FF_ENABLE_DEPRECATION_WARNINGS
 
 find_stream_info_err:
     for (i = 0; i < ic->nb_streams; i++) {
+        avcodec_close(ic->streams[i]->internal->avctx);
         av_freep(&ic->streams[i]->info);
         av_bsf_free(&ic->streams[i]->internal->extract_extradata.bsf);
         av_packet_free(&ic->streams[i]->internal->extract_extradata.pkt);