diff mbox series

[FFmpeg-devel,3/4] avformat/utils: Free new streams in ff_add_attached_pic on error

Message ID HE1PR0301MB215497CA031D0E43498ACB808F7E9@HE1PR0301MB2154.eurprd03.prod.outlook.com
State Accepted
Commit 7aee4762d336a10e5ef51a32a449efaa349e9d07
Headers show
Series [FFmpeg-devel,1/4] avformat: Add and use helper function to add attachment streams
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt March 29, 2021, 8:42 a.m. UTC
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
mov chapter tracks are not compatible with this.

 libavformat/internal.h | 3 +++
 libavformat/utils.c    | 9 +++++++--
 2 files changed, 10 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/internal.h b/libavformat/internal.h
index b3c5d8a1d5..a6987619f7 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -678,6 +678,9 @@  int ff_read_packet(AVFormatContext *s, AVPacket *pkt);
  * @param buf  if set, it contains the data and size information to be used
  *             for the attached pic; if unset, data is read from pb.
  * @param size the size of the data to read if buf is unset.
+ *
+ * @return 0 on success, < 0 on error. On error, this function removes
+ *         the stream it has added (if any).
  */
 int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb,
                         AVBufferRef **buf, int size);
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 2bd7dd8ec7..67302907b8 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -474,9 +474,10 @@  int avformat_queue_attached_pictures(AVFormatContext *s)
     return 0;
 }
 
-int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb,
+int ff_add_attached_pic(AVFormatContext *s, AVStream *st0, AVIOContext *pb,
                         AVBufferRef **buf, int size)
 {
+    AVStream *st = st0;
     AVPacket *pkt;
     int ret;
 
@@ -493,7 +494,7 @@  int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb,
     } else {
         ret = av_get_packet(pb, pkt, size);
         if (ret < 0)
-            return ret;
+            goto fail;
     }
     st->disposition         |= AV_DISPOSITION_ATTACHED_PIC;
     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
@@ -502,6 +503,10 @@  int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb,
     pkt->flags       |= AV_PKT_FLAG_KEY;
 
     return 0;
+fail:
+    if (!st0)
+        ff_free_stream(s, st);
+    return ret;
 }
 
 static int update_stream_avctx(AVFormatContext *s)