diff mbox series

[FFmpeg-devel,3/4] avformat/mpegenc: Avoid adding invalid packet to queue

Message ID 20210215145423.591629-2-andreas.rheinhardt@gmail.com
State Accepted
Commit 4294f64d578e14b3f65a6a2cd064461cb74bb122
Headers show
Series [FFmpeg-devel,1/4] avformat/mpegenc: Ensure packet queue stays valid | expand

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 Feb. 15, 2021, 2:54 p.m. UTC
Do this by moving the check before the allocation.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavformat/mpegenc.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c
index 1204e57f04..14b904b152 100644
--- a/libavformat/mpegenc.c
+++ b/libavformat/mpegenc.c
@@ -1179,18 +1179,6 @@  static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
     av_log(ctx, AV_LOG_TRACE, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
             dts / 90000.0, pts / 90000.0, pkt->flags,
             pkt->stream_index, pts != AV_NOPTS_VALUE);
-    pkt_desc                 = av_mallocz(sizeof(PacketDesc));
-    if (!pkt_desc)
-        return AVERROR(ENOMEM);
-    if (!stream->predecode_packet) {
-        stream->predecode_packet  = pkt_desc;
-    } else
-        stream->last_packet->next = pkt_desc;
-    stream->last_packet = pkt_desc;
-    if (!stream->premux_packet)
-        stream->premux_packet = pkt_desc;
-    pkt_desc->pts            = pts;
-    pkt_desc->dts            = dts;
 
     if (st->codecpar->codec_id == AV_CODEC_ID_PCM_DVD) {
         if (size < 3) {
@@ -1204,6 +1192,18 @@  static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
         size -= 3;
     }
 
+    pkt_desc                 = av_mallocz(sizeof(PacketDesc));
+    if (!pkt_desc)
+        return AVERROR(ENOMEM);
+    if (!stream->predecode_packet) {
+        stream->predecode_packet  = pkt_desc;
+    } else
+        stream->last_packet->next = pkt_desc;
+    stream->last_packet = pkt_desc;
+    if (!stream->premux_packet)
+        stream->premux_packet = pkt_desc;
+    pkt_desc->pts            = pts;
+    pkt_desc->dts            = dts;
     pkt_desc->unwritten_size =
     pkt_desc->size           = size;