diff mbox series

[FFmpeg-devel,5/7] fftools/ffmpeg: Avoid allocating+freeing frame, check allocations

Message ID AM7PR03MB6660EB6AF3E3C2C180CD102F8F6A9@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit b886512ef2503fc585c302484be71475d3100480
Headers show
Series [FFmpeg-devel,1/4] fftools/cmdutils: Atomically add elements to list of pointers, fix crash
Related show

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Dec. 3, 2021, 8:45 p.m. UTC
Fixes a potential crash upon av_frame_alloc() failure.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 fftools/ffmpeg.c     | 11 ++++-------
 fftools/ffmpeg_opt.c |  4 ++++
 2 files changed, 8 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index cfb04d5eff..8c3a4f7c0c 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1291,7 +1291,7 @@  static void do_video_out(OutputFile *of,
         int forced_keyframe = 0;
         double pts_time;
 
-        if (i < nb0_frames && ost->last_frame) {
+        if (i < nb0_frames && ost->last_frame->buf[0]) {
             in_picture = ost->last_frame;
         } else
             in_picture = next_picture;
@@ -1419,13 +1419,10 @@  static void do_video_out(OutputFile *of,
             do_video_stats(ost, frame_size);
     }
 
-    if (!ost->last_frame)
-        ost->last_frame = av_frame_alloc();
     av_frame_unref(ost->last_frame);
-    if (next_picture && ost->last_frame)
-        av_frame_ref(ost->last_frame, next_picture);
-    else
-        av_frame_free(&ost->last_frame);
+    if (next_picture)
+        if (av_frame_ref(ost->last_frame, next_picture) < 0)
+            goto error;
 
     return;
 error:
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 17e4cdd2d6..6d091d3e23 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1903,6 +1903,10 @@  static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in
         ost->avfilter = get_ost_filters(o, oc, ost);
         if (!ost->avfilter)
             exit_program(1);
+
+        ost->last_frame = av_frame_alloc();
+        if (!ost->last_frame)
+            exit_program(1);
     } else {
         MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i, ost->copy_initial_nonkeyframes, oc ,st);
     }