[FFmpeg-devel] [PATCH 2/3] fftools: Use report_error_then_exit_program() for allocation failures

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Sat Aug 27 16:58:46 EEST 2022


Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 fftools/cmdutils.c      | 21 +++++--------
 fftools/ffmpeg.c        | 25 ++++++---------
 fftools/ffmpeg_filter.c | 10 +++---
 fftools/ffmpeg_opt.c    | 70 ++++++++++++++++-------------------------
 fftools/ffprobe.c       |  6 ++--
 fftools/opt_common.c    |  6 ++--
 6 files changed, 52 insertions(+), 86 deletions(-)

diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index b6f5c01ca3..47568dcebc 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -656,7 +656,7 @@ static void init_parse_context(OptionParseContext *octx,
     octx->nb_groups = nb_groups;
     octx->groups    = av_calloc(octx->nb_groups, sizeof(*octx->groups));
     if (!octx->groups)
-        exit_program(1);
+        report_error_then_exit_program(AVERROR(ENOMEM));
 
     for (i = 0; i < octx->nb_groups; i++)
         octx->groups[i].group_def = &groups[i];
@@ -964,11 +964,8 @@ AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
     if (!s->nb_streams)
         return NULL;
     opts = av_calloc(s->nb_streams, sizeof(*opts));
-    if (!opts) {
-        av_log(NULL, AV_LOG_ERROR,
-               "Could not alloc memory for stream options.\n");
-        exit_program(1);
-    }
+    if (!opts)
+        report_error_then_exit_program(AVERROR(ENOMEM));
     for (i = 0; i < s->nb_streams; i++)
         opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
                                     s, s->streams[i], NULL);
@@ -983,10 +980,8 @@ void *grow_array(void *array, int elem_size, int *size, int new_size)
     }
     if (*size < new_size) {
         uint8_t *tmp = av_realloc_array(array, new_size, elem_size);
-        if (!tmp) {
-            av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
-            exit_program(1);
-        }
+        if (!tmp)
+            report_error_then_exit_program(AVERROR(ENOMEM));
         memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
         *size = new_size;
         return tmp;
@@ -999,10 +994,8 @@ void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)
     void *new_elem;
 
     if (!(new_elem = av_mallocz(elem_size)) ||
-        av_dynarray_add_nofree(ptr, nb_elems, new_elem) < 0) {
-        av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
-        exit_program(1);
-    }
+        av_dynarray_add_nofree(ptr, nb_elems, new_elem) < 0)
+        report_error_then_exit_program(AVERROR(ENOMEM));
     return new_elem;
 }
 
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index ef7177fc33..18b91e3ef6 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1094,10 +1094,8 @@ static void do_subtitle_out(OutputFile *of,
             return;
 
         ret = av_new_packet(pkt, subtitle_out_max_size);
-        if (ret < 0) {
-            av_log(NULL, AV_LOG_FATAL, "Failed to allocate subtitle encode buffer\n");
-            exit_program(1);
-        }
+        if (ret < 0)
+            report_error_then_exit_program(AVERROR(ENOMEM));
 
         sub->pts = pts;
         // start_display_time is required to be 0
@@ -2345,7 +2343,7 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
         if (!ist->sub2video.sub_queue)
             ist->sub2video.sub_queue = av_fifo_alloc2(8, sizeof(AVSubtitle), AV_FIFO_FLAG_AUTO_GROW);
         if (!ist->sub2video.sub_queue)
-            exit_program(1);
+            report_error_then_exit_program(AVERROR(ENOMEM));
 
         ret = av_fifo_write(ist->sub2video.sub_queue, &subtitle, 1);
         if (ret < 0)
@@ -2874,7 +2872,7 @@ static void set_encoder_id(OutputFile *of, OutputStream *ost)
     encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2;
     encoder_string     = av_mallocz(encoder_string_len);
     if (!encoder_string)
-        exit_program(1);
+        report_error_then_exit_program(AVERROR(ENOMEM));
 
     if (!of->bitexact && !ost->bitexact)
         av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
@@ -2897,10 +2895,8 @@ static void parse_forced_key_frames(char *kf, OutputStream *ost,
             n++;
     size = n;
     pts = av_malloc_array(size, sizeof(*pts));
-    if (!pts) {
-        av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
-        exit_program(1);
-    }
+    if (!pts)
+        report_error_then_exit_program(AVERROR(ENOMEM));
 
     p = kf;
     for (i = 0; i < n; i++) {
@@ -2919,11 +2915,8 @@ static void parse_forced_key_frames(char *kf, OutputStream *ost,
 
             if (nb_ch > INT_MAX - size ||
                 !(pts = av_realloc_f(pts, size += nb_ch - 1,
-                                     sizeof(*pts)))) {
-                av_log(NULL, AV_LOG_FATAL,
-                       "Could not allocate forced key frames array.\n");
-                exit_program(1);
-            }
+                                     sizeof(*pts))))
+                report_error_then_exit_program(AVERROR(ENOMEM));
             t = p[8] ? parse_time_or_die("force_key_frames", p + 8, 1) : 0;
             t = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
 
@@ -3861,7 +3854,7 @@ static int process_input(int file_index)
 
             dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
             if (!dst_data)
-                exit_program(1);
+                report_error_then_exit_program(AVERROR(ENOMEM));
 
             memcpy(dst_data, src_sd->data, src_sd->size);
         }
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 16622e49c1..26590000c7 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -118,7 +118,7 @@ static const char *choose_pix_fmts(OutputFilter *ofilter, AVBPrint *bprint)
             av_bprintf(bprint, "%s%c", name, p[1] == AV_PIX_FMT_NONE ? '\0' : '|');
         }
         if (!av_bprint_is_complete(bprint))
-            exit_program(1);
+            report_error_then_exit_program(AVERROR(ENOMEM));
         return bprint->str;
     } else
         return NULL;
@@ -182,7 +182,7 @@ int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
     InputFilter  *ifilter;
 
     if (!fg)
-        exit_program(1);
+        report_error_then_exit_program(AVERROR(ENOMEM));
     fg->index = nb_filtergraphs;
 
     ofilter = ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
@@ -199,7 +199,7 @@ int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
 
     ifilter->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
     if (!ifilter->frame_queue)
-        exit_program(1);
+        report_error_then_exit_program(AVERROR(ENOMEM));
 
     GROW_ARRAY(ist->filters, ist->nb_filters);
     ist->filters[ist->nb_filters - 1] = ifilter;
@@ -223,7 +223,7 @@ static char *describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in)
         res = av_asprintf("%s:%s", ctx->filter->name,
                           avfilter_pad_get_name(pads, inout->pad_idx));
     if (!res)
-        exit_program(1);
+        report_error_then_exit_program(AVERROR(ENOMEM));
     return res;
 }
 
@@ -308,7 +308,7 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
 
     ifilter->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
     if (!ifilter->frame_queue)
-        exit_program(1);
+        report_error_then_exit_program(AVERROR(ENOMEM));
 
     GROW_ARRAY(ist->filters, ist->nb_filters);
     ist->filters[ist->nb_filters - 1] = ifilter;
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 8128dcf9fb..b0b8ecc868 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -996,7 +996,7 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
             if (hwaccel_device) {
                 ist->hwaccel_device = av_strdup(hwaccel_device);
                 if (!ist->hwaccel_device)
-                    exit_program(1);
+                    report_error_then_exit_program(AVERROR(ENOMEM));
             }
 
             ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
@@ -1027,10 +1027,8 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
         ist->prev_pkt_pts = AV_NOPTS_VALUE;
 
         ist->dec_ctx = avcodec_alloc_context3(ist->dec);
-        if (!ist->dec_ctx) {
-            av_log(NULL, AV_LOG_ERROR, "Error allocating the decoder context.\n");
-            exit_program(1);
-        }
+        if (!ist->dec_ctx)
+            report_error_then_exit_program(AVERROR(ENOMEM));
 
         ret = avcodec_parameters_to_context(ist->dec_ctx, par);
         if (ret < 0) {
@@ -1040,11 +1038,11 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
 
         ist->decoded_frame = av_frame_alloc();
         if (!ist->decoded_frame)
-            exit_program(1);
+            report_error_then_exit_program(AVERROR(ENOMEM));
 
         ist->pkt = av_packet_alloc();
         if (!ist->pkt)
-            exit_program(1);
+            report_error_then_exit_program(AVERROR(ENOMEM));
 
         if (o->bitexact)
             ist->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
@@ -1094,7 +1092,7 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
 
         ist->par = avcodec_parameters_alloc();
         if (!ist->par)
-            exit_program(1);
+            report_error_then_exit_program(AVERROR(ENOMEM));
 
         ret = avcodec_parameters_from_context(ist->par, ist->dec_ctx);
         if (ret < 0) {
@@ -1224,10 +1222,8 @@ static int open_input_file(OptionsContext *o, const char *filename)
 
     /* get default parameters from command line */
     ic = avformat_alloc_context();
-    if (!ic) {
-        print_error(filename, AVERROR(ENOMEM));
-        exit_program(1);
-    }
+    if (!ic)
+        report_error_then_exit_program(AVERROR(ENOMEM));
     if (o->nb_audio_sample_rate) {
         av_dict_set_int(&o->g->format_opts, "sample_rate", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i, 0);
     }
@@ -1476,10 +1472,8 @@ static char *get_line(AVIOContext *s, AVBPrint *bprint)
     while ((c = avio_r8(s)) && c != '\n')
         av_bprint_chars(bprint, c, 1);
 
-    if (!av_bprint_is_complete(bprint)) {
-        av_log(NULL, AV_LOG_FATAL, "Could not alloc buffer for reading preset.\n");
-        exit_program(1);
-    }
+    if (!av_bprint_is_complete(bprint))
+        report_error_then_exit_program(AVERROR(ENOMEM));
     return bprint->str;
 }
 
@@ -1567,10 +1561,8 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e
     double qscale = -1;
     int i;
 
-    if (!st) {
-        av_log(NULL, AV_LOG_FATAL, "Could not alloc stream.\n");
-        exit_program(1);
-    }
+    if (!st)
+        report_error_then_exit_program(AVERROR(ENOMEM));
 
     if (oc->nb_streams - 1 < o->nb_streamid_map)
         st->id = o->streamid_map[oc->nb_streams - 1];
@@ -1592,19 +1584,17 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e
 
     if (ost->enc) {
         ost->enc_ctx = avcodec_alloc_context3(ost->enc);
-        if (!ost->enc_ctx) {
-            av_log(NULL, AV_LOG_ERROR, "Error allocating the encoding context.\n");
-            exit_program(1);
-        }
+        if (!ost->enc_ctx)
+            report_error_then_exit_program(AVERROR(ENOMEM));
     }
 
     ost->filtered_frame = av_frame_alloc();
     if (!ost->filtered_frame)
-        exit_program(1);
+        report_error_then_exit_program(AVERROR(ENOMEM));
 
     ost->pkt = av_packet_alloc();
     if (!ost->pkt)
-        exit_program(1);
+        report_error_then_exit_program(AVERROR(ENOMEM));
 
     if (ost->enc) {
         AVIOContext *s = NULL;
@@ -1897,28 +1887,22 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in
 
         MATCH_PER_STREAM_OPT(intra_matrices, str, intra_matrix, oc, st);
         if (intra_matrix) {
-            if (!(video_enc->intra_matrix = av_mallocz(sizeof(*video_enc->intra_matrix) * 64))) {
-                av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for intra matrix.\n");
-                exit_program(1);
-            }
+            if (!(video_enc->intra_matrix = av_mallocz(sizeof(*video_enc->intra_matrix) * 64)))
+                report_error_then_exit_program(AVERROR(ENOMEM));
             parse_matrix_coeffs(video_enc->intra_matrix, intra_matrix);
         }
         MATCH_PER_STREAM_OPT(chroma_intra_matrices, str, chroma_intra_matrix, oc, st);
         if (chroma_intra_matrix) {
             uint16_t *p = av_mallocz(sizeof(*video_enc->chroma_intra_matrix) * 64);
-            if (!p) {
-                av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for intra matrix.\n");
-                exit_program(1);
-            }
+            if (!p)
+                report_error_then_exit_program(AVERROR(ENOMEM));
             video_enc->chroma_intra_matrix = p;
             parse_matrix_coeffs(p, chroma_intra_matrix);
         }
         MATCH_PER_STREAM_OPT(inter_matrices, str, inter_matrix, oc, st);
         if (inter_matrix) {
-            if (!(video_enc->inter_matrix = av_mallocz(sizeof(*video_enc->inter_matrix) * 64))) {
-                av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for inter matrix.\n");
-                exit_program(1);
-            }
+            if (!(video_enc->inter_matrix = av_mallocz(sizeof(*video_enc->inter_matrix) * 64)))
+                report_error_then_exit_program(AVERROR(ENOMEM));
             parse_matrix_coeffs(video_enc->inter_matrix, inter_matrix);
         }
 
@@ -1975,7 +1959,7 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in
         MATCH_PER_STREAM_OPT(passlogfiles, str, ost->logfile_prefix, oc, st);
         if (ost->logfile_prefix &&
             !(ost->logfile_prefix = av_strdup(ost->logfile_prefix)))
-            exit_program(1);
+            report_error_then_exit_program(AVERROR(ENOMEM));
 
         if (do_pass) {
             char logfilename[1024];
@@ -2055,7 +2039,7 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in
 
         ost->last_frame = av_frame_alloc();
         if (!ost->last_frame)
-            exit_program(1);
+            report_error_then_exit_program(AVERROR(ENOMEM));
     } else
         check_streamcopy_filters(o, oc, ost, AVMEDIA_TYPE_VIDEO);
 
@@ -2146,7 +2130,7 @@ static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc, in
                                           ost->audio_channels_mapped + 1,
                                           sizeof(*ost->audio_channels_map)
                                           ) < 0 )
-                        exit_program(1);
+                        report_error_then_exit_program(AVERROR(ENOMEM));
 
                     ost->audio_channels_map[ost->audio_channels_mapped++] = map->channel_idx;
                 }
@@ -2875,7 +2859,7 @@ static void set_channel_layout(OutputFilter *f, OutputStream *ost)
         /* Pass the layout through for all orders but UNSPEC */
         err = av_channel_layout_copy(&f->ch_layout, &ost->enc_ctx->ch_layout);
         if (err < 0)
-            exit_program(1);
+            report_error_then_exit_program(AVERROR(ENOMEM));
         return;
     }
 
@@ -2896,7 +2880,7 @@ static void set_channel_layout(OutputFilter *f, OutputStream *ost)
         /* Use it if one is found */
         err = av_channel_layout_copy(&f->ch_layout, &ost->enc->ch_layouts[i]);
         if (err < 0)
-            exit_program(1);
+            report_error_then_exit_program(AVERROR(ENOMEM));
         return;
     }
     /* If no layout for the amount of channels requested was found, use the default
diff --git a/fftools/ffprobe.c b/fftools/ffprobe.c
index 8983dc28cc..7b15e9d3f7 100644
--- a/fftools/ffprobe.c
+++ b/fftools/ffprobe.c
@@ -3291,10 +3291,8 @@ static int open_input_file(InputFile *ifile, const char *filename,
     int scan_all_pmts_set = 0;
 
     fmt_ctx = avformat_alloc_context();
-    if (!fmt_ctx) {
-        print_error(filename, AVERROR(ENOMEM));
-        exit_program(1);
-    }
+    if (!fmt_ctx)
+        report_error_then_exit_program(AVERROR(ENOMEM));
 
     if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
         av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
diff --git a/fftools/opt_common.c b/fftools/opt_common.c
index ae5e28a5af..fe1e0f4f1b 100644
--- a/fftools/opt_common.c
+++ b/fftools/opt_common.c
@@ -639,10 +639,8 @@ static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
 
     while ((desc = avcodec_descriptor_next(desc)))
         nb_codecs++;
-    if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
-        av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
-        exit_program(1);
-    }
+    if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs))))
+        report_error_then_exit_program(AVERROR(ENOMEM));
     desc = NULL;
     while ((desc = avcodec_descriptor_next(desc)))
         codecs[i++] = desc;
-- 
2.34.1



More information about the ffmpeg-devel mailing list