[FFmpeg-cvslog] avformat/matroskadec: Check before allocations

Andreas Rheinhardt git at videolan.org
Wed May 20 08:37:26 EEST 2020


ffmpeg | branch: release/4.2 | Andreas Rheinhardt <andreas.rheinhardt at gmail.com> | Tue Dec  3 18:09:03 2019 +0100| [703473ec04311016d2a1636ae9a91d2a1234eb91] | committer: Andreas Rheinhardt

avformat/matroskadec: Check before allocations

That way one doesn't have to free later. In this case (concerning TTA
extradata), this also fixes a memleak when the output samplerate is
invalid.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
Signed-off-by: James Almer <jamrial at gmail.com>
(cherry picked from commit f7bf59b431e0921d5f318154f64c78bb226e33b9)

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=703473ec04311016d2a1636ae9a91d2a1234eb91
---

 libavformat/matroskadec.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index a43cc0bf19..724d81b654 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -2558,30 +2558,29 @@ static int matroska_parse_tracks(AVFormatContext *s)
             memcpy(&extradata[12], track->codec_priv.data,
                    track->codec_priv.size);
         } else if (codec_id == AV_CODEC_ID_TTA) {
-            extradata_size = 30;
-            extradata      = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
-            if (!extradata)
-                return AVERROR(ENOMEM);
-            ffio_init_context(&b, extradata, extradata_size, 1,
-                              NULL, NULL, NULL, NULL);
-            avio_write(&b, "TTA1", 4);
-            avio_wl16(&b, 1);
             if (track->audio.channels > UINT16_MAX ||
                 track->audio.bitdepth > UINT16_MAX) {
                 av_log(matroska->ctx, AV_LOG_WARNING,
                        "Too large audio channel number %"PRIu64
                        " or bitdepth %"PRIu64". Skipping track.\n",
                        track->audio.channels, track->audio.bitdepth);
-                av_freep(&extradata);
                 if (matroska->ctx->error_recognition & AV_EF_EXPLODE)
                     return AVERROR_INVALIDDATA;
                 else
                     continue;
             }
-            avio_wl16(&b, track->audio.channels);
-            avio_wl16(&b, track->audio.bitdepth);
             if (track->audio.out_samplerate < 0 || track->audio.out_samplerate > INT_MAX)
                 return AVERROR_INVALIDDATA;
+            extradata_size = 30;
+            extradata      = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
+            if (!extradata)
+                return AVERROR(ENOMEM);
+            ffio_init_context(&b, extradata, extradata_size, 1,
+                              NULL, NULL, NULL, NULL);
+            avio_write(&b, "TTA1", 4);
+            avio_wl16(&b, 1);
+            avio_wl16(&b, track->audio.channels);
+            avio_wl16(&b, track->audio.bitdepth);
             avio_wl32(&b, track->audio.out_samplerate);
             avio_wl32(&b, av_rescale((matroska->duration * matroska->time_scale),
                                      track->audio.out_samplerate,



More information about the ffmpeg-cvslog mailing list