[FFmpeg-devel] [PATCH] avformat/matroskaenc: Fix memleak

Andreas Rheinhardt andreas.rheinhardt at googlemail.com
Thu Apr 4 19:27:03 EEST 2019

Audio packets with size zero, but with side-data currently lead to
memleaks, in the Matroska muxer, because they are not properly freed:

They are currently put into an AVPacket in the MatroskaMuxContext to
ensure that the necessary audio is always available for a new cluster,
but are only written and freed when their size is > 0.

As the only use we have for such packets consists in updating the
CodecPrivate it makes no sense to store these packets at all and this
is how this commit solves the memleak.

Fixes track ticket #7827.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at googlemail.com>

Could some kind soul please run Valgrind to check that it really fixes
the memleak from #7827?
(I have thought about whether all zero-sized packets (not necessarily
audio packets only) should be discarded at this point, but if I am not
mistaken, then WebVTT subtitles can be side-data-only.)

 libavformat/matroskaenc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index b9f99c4463..06f3aeb46d 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -2534,7 +2534,8 @@ static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
     // buffer an audio packet to ensure the packet containing the video
     // keyframe's timecode is contained in the same cluster for WebM
     if (codec_type == AVMEDIA_TYPE_AUDIO) {
-        ret = av_packet_ref(&mkv->cur_audio_pkt, pkt);
+        if (pkt->size > 0)
+            ret = av_packet_ref(&mkv->cur_audio_pkt, pkt);
     } else
         ret = mkv_write_packet_internal(s, pkt, 0);
     return ret;

More information about the ffmpeg-devel mailing list