[FFmpeg-devel,1/3] avformat/utils: Fix memleaks

Submitted by Andreas Rheinhardt on Aug. 16, 2019, 4:43 p.m.

Details

Message ID 20190816164314.57764-1-andreas.rheinhardt@gmail.com
State New
Headers show

Commit Message

Andreas Rheinhardt Aug. 16, 2019, 4:43 p.m.
ff_read_packet had several potential memleaks:
1. If av_packet_make_refcounted fails, it means that the packet is not
refcounted, but it could nevertheless carry side data and therefore
needs to be unreferenced.
2. If a packet happens to have an illegal stream index (i.e. one that
does not correspond to a stream), it should nevertheless be
unreferenced.
3. If putting a packet on a packet list fails, it wasn't unreferenced.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavformat/utils.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

Comments

Andreas Rheinhardt Aug. 19, 2019, 10:04 p.m.
Andreas Rheinhardt:
> ff_read_packet had several potential memleaks:
> 1. If av_packet_make_refcounted fails, it means that the packet is not
> refcounted, but it could nevertheless carry side data and therefore
> needs to be unreferenced.
> 2. If a packet happens to have an illegal stream index (i.e. one that
> does not correspond to a stream), it should nevertheless be
> unreferenced.
> 3. If putting a packet on a packet list fails, it wasn't unreferenced.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
This patchset has been superseded by
https://ffmpeg.org/pipermail/ffmpeg-devel/2019-August/248560.html.

- Andreas

Patch hide | download patch | download mbox

diff --git a/libavformat/utils.c b/libavformat/utils.c
index b57e680089..a902841ad5 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -873,8 +873,10 @@  int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
         }
 
         err = av_packet_make_refcounted(pkt);
-        if (err < 0)
+        if (err < 0) {
+            av_packet_unref(pkt);
             return err;
+        }
 
         if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) &&
             (pkt->flags & AV_PKT_FLAG_CORRUPT)) {
@@ -887,6 +889,7 @@  int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
 
         if (pkt->stream_index >= (unsigned)s->nb_streams) {
             av_log(s, AV_LOG_ERROR, "Invalid stream index %d\n", pkt->stream_index);
+            av_packet_unref(pkt);
             continue;
         }
 
@@ -917,8 +920,10 @@  int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
         err = ff_packet_list_put(&s->internal->raw_packet_buffer,
                                  &s->internal->raw_packet_buffer_end,
                                  pkt, 0);
-        if (err)
+        if (err < 0) {
+            av_packet_unref(pkt);
             return err;
+        }
         s->internal->raw_packet_buffer_remaining_size -= pkt->size;
 
         if ((err = probe_codec(s, st, pkt)) < 0)