1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

fftools/cmdutils: add error handling to allocate_array_elem()

This commit is contained in:
Anton Khirnov 2023-07-13 17:50:16 +02:00
parent 8eb5adeab8
commit 6be4a29397
5 changed files with 51 additions and 14 deletions

View File

@ -1002,7 +1002,7 @@ void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)
if (!(new_elem = av_mallocz(elem_size)) ||
av_dynarray_add_nofree(ptr, nb_elems, new_elem) < 0)
report_and_exit(AVERROR(ENOMEM));
return NULL;
return new_elem;
}

View File

@ -430,13 +430,12 @@ void *grow_array(void *array, int elem_size, int *size, int new_size);
* Atomically add a new element to an array of pointers, i.e. allocate
* a new entry, reallocate the array of pointers and make the new last
* member of this array point to the newly allocated buffer.
* Calls exit() on failure.
*
* @param array array of pointers to reallocate
* @param elem_size size of the new element to allocate
* @param nb_elems pointer to the number of elements of the array array;
* *nb_elems will be incremented by one by this function.
* @return pointer to the newly allocated entry
* @return pointer to the newly allocated entry or NULL on failure
*/
void *allocate_array_elem(void *array, size_t elem_size, int *nb_elems);

View File

@ -1018,8 +1018,11 @@ static DemuxStream *demux_stream_alloc(Demuxer *d, AVStream *st)
{
const char *type_str = av_get_media_type_string(st->codecpar->codec_type);
InputFile *f = &d->f;
DemuxStream *ds = allocate_array_elem(&f->streams, sizeof(*ds),
&f->nb_streams);
DemuxStream *ds;
ds = allocate_array_elem(&f->streams, sizeof(*ds), &f->nb_streams);
if (!ds)
return NULL;
ds->ist.st = st;
ds->ist.file_index = f->index;
@ -1051,6 +1054,9 @@ static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
int ret;
ds = demux_stream_alloc(d, st);
if (!ds)
return AVERROR(ENOMEM);
ist = &ds->ist;
ist->discard = 1;
@ -1328,6 +1334,9 @@ static Demuxer *demux_alloc(void)
{
Demuxer *d = allocate_array_elem(&input_files, sizeof(*d), &nb_input_files);
if (!d)
return NULL;
d->f.class = &input_file_class;
d->f.index = nb_input_files - 1;
@ -1358,6 +1367,9 @@ int ifile_open(const OptionsContext *o, const char *filename)
int64_t recording_time = o->recording_time;
d = demux_alloc();
if (!d)
return AVERROR(ENOMEM);
f = &d->f;
if (stop_time != INT64_MAX && recording_time != INT64_MAX) {

View File

@ -550,8 +550,10 @@ static OutputFilter *ofilter_alloc(FilterGraph *fg)
OutputFilterPriv *ofp;
OutputFilter *ofilter;
ofp = allocate_array_elem(&fg->outputs, sizeof(*ofp),
&fg->nb_outputs);
ofp = allocate_array_elem(&fg->outputs, sizeof(*ofp), &fg->nb_outputs);
if (!ofp)
return NULL;
ofilter = &ofp->ofilter;
ofilter->graph = fg;
ofp->format = -1;
@ -715,10 +717,14 @@ int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost)
static InputFilter *ifilter_alloc(FilterGraph *fg)
{
InputFilterPriv *ifp = allocate_array_elem(&fg->inputs, sizeof(*ifp),
&fg->nb_inputs);
InputFilter *ifilter = &ifp->ifilter;
InputFilterPriv *ifp;
InputFilter *ifilter;
ifp = allocate_array_elem(&fg->inputs, sizeof(*ifp), &fg->nb_inputs);
if (!ifp)
return NULL;
ifilter = &ifp->ifilter;
ifilter->graph = fg;
ifp->frame = av_frame_alloc();
@ -800,13 +806,18 @@ static const AVClass fg_class = {
int fg_create(FilterGraph **pfg, char *graph_desc)
{
FilterGraphPriv *fgp = allocate_array_elem(&filtergraphs, sizeof(*fgp), &nb_filtergraphs);
FilterGraph *fg = &fgp->fg;
FilterGraphPriv *fgp;
FilterGraph *fg;
AVFilterInOut *inputs, *outputs;
AVFilterGraph *graph;
int ret = 0;
fgp = allocate_array_elem(&filtergraphs, sizeof(*fgp), &nb_filtergraphs);
if (!fgp)
return AVERROR(ENOMEM);
fg = &fgp->fg;
if (pfg)
*pfg = fg;
@ -851,6 +862,9 @@ int fg_create(FilterGraph **pfg, char *graph_desc)
for (AVFilterInOut *cur = outputs; cur; cur = cur->next) {
OutputFilter *const ofilter = ofilter_alloc(fg);
if (!ofilter)
goto fail;
ofilter->linklabel = cur->name;
cur->name = NULL;

View File

@ -413,8 +413,11 @@ static const AVClass output_stream_class = {
static MuxStream *mux_stream_alloc(Muxer *mux, enum AVMediaType type)
{
const char *type_str = av_get_media_type_string(type);
MuxStream *ms = allocate_array_elem(&mux->of.streams, sizeof(*ms),
&mux->of.nb_streams);
MuxStream *ms;
ms = allocate_array_elem(&mux->of.streams, sizeof(*ms), &mux->of.nb_streams);
if (!ms)
return NULL;
ms->ost.file_index = mux->of.index;
ms->ost.index = mux->of.nb_streams - 1;
@ -1101,6 +1104,9 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
return AVERROR(ENOMEM);
ms = mux_stream_alloc(mux, type);
if (!ms)
return AVERROR(ENOMEM);
ost = &ms->ost;
if (o->streamid) {
@ -2557,6 +2563,9 @@ static Muxer *mux_alloc(void)
{
Muxer *mux = allocate_array_elem(&output_files, sizeof(*mux), &nb_output_files);
if (!mux)
return NULL;
mux->of.class = &output_file_class;
mux->of.index = nb_output_files - 1;
@ -2576,6 +2585,9 @@ int of_open(const OptionsContext *o, const char *filename)
int64_t stop_time = o->stop_time;
mux = mux_alloc();
if (!mux)
return AVERROR(ENOMEM);
of = &mux->of;
if (stop_time != INT64_MAX && recording_time != INT64_MAX) {