[FFmpeg-devel] [PATCH 3/5] avcodec/libvorbisenc: Fix memleak upon error

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Thu Apr 15 05:04:09 EEST 2021


The vorbis_comment struct used to during codec initialization
would leak in case an error happened after its initialization
(e.g. if the allocation of extradata failed). This has been fixed.

Given that said struct is only used when writing the header, it has
also been moved from the context.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/libvorbisenc.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/libavcodec/libvorbisenc.c b/libavcodec/libvorbisenc.c
index 9d36457328..b556280a95 100644
--- a/libavcodec/libvorbisenc.c
+++ b/libavcodec/libvorbisenc.c
@@ -47,7 +47,6 @@ typedef struct LibvorbisEncContext {
     AVFifoBuffer *pkt_fifo;             /**< output packet buffer           */
     int eof;                            /**< end-of-file flag               */
     int dsp_initialized;                /**< vd has been initialized        */
-    vorbis_comment vc;                  /**< VorbisComment info             */
     double iblock;                      /**< impulse block bias option      */
     AVVorbisParseContext *vp;           /**< parse context to get durations */
     AudioFrameQueue afq;                /**< frame queue for timestamps     */
@@ -206,6 +205,7 @@ static av_cold int libvorbis_encode_init(AVCodecContext *avctx)
 {
     LibvorbisEncContext *s = avctx->priv_data;
     ogg_packet header, header_comm, header_code;
+    vorbis_comment vc;
     uint8_t *p;
     unsigned int offset;
     int ret;
@@ -227,12 +227,14 @@ static av_cold int libvorbis_encode_init(AVCodecContext *avctx)
         goto error;
     }
 
-    vorbis_comment_init(&s->vc);
+    vorbis_comment_init(&vc);
     if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
-        vorbis_comment_add_tag(&s->vc, "encoder", LIBAVCODEC_IDENT);
+        vorbis_comment_add_tag(&vc, "encoder", LIBAVCODEC_IDENT);
 
-    if ((ret = vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm,
-                                         &header_code))) {
+    ret = vorbis_analysis_headerout(&s->vd, &vc, &header, &header_comm,
+                                    &header_code);
+    vorbis_comment_clear(&vc);
+    if (ret) {
         ret = vorbis_error_to_averror(ret);
         goto error;
     }
@@ -264,8 +266,6 @@ static av_cold int libvorbis_encode_init(AVCodecContext *avctx)
         return ret;
     }
 
-    vorbis_comment_clear(&s->vc);
-
     avctx->frame_size = LIBVORBIS_FRAME_SIZE;
     ff_af_queue_init(avctx, &s->afq);
 
-- 
2.27.0



More information about the ffmpeg-devel mailing list