fftools/cmdutils: add error handling to grow_array()

This commit is contained in:
Anton Khirnov 2023-07-14 12:28:18 +02:00
parent 6be4a29397
commit b23abb7c48
2 changed files with 21 additions and 12 deletions

View File

@ -262,6 +262,7 @@ static int write_option(void *optctx, const OptionDef *po, const char *opt,
void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
(uint8_t *)optctx + po->u.off : po->u.dst_ptr;
int *dstcount;
int ret;
if (po->flags & OPT_SPEC) {
SpecifierOpt **so = dst;
@ -269,7 +270,10 @@ static int write_option(void *optctx, const OptionDef *po, const char *opt,
char *str;
dstcount = (int *)(so + 1);
*so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
ret = grow_array((void**)so, sizeof(**so), dstcount, *dstcount + 1);
if (ret < 0)
return ret;
str = av_strdup(p ? p + 1 : "");
if (!str)
return AVERROR(ENOMEM);
@ -979,21 +983,22 @@ int setup_find_stream_info_opts(AVFormatContext *s,
return 0;
}
void *grow_array(void *array, int elem_size, int *size, int new_size)
int grow_array(void **array, int elem_size, int *size, int new_size)
{
if (new_size >= INT_MAX / elem_size) {
av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
exit_program(1);
return AVERROR(ERANGE);
}
if (*size < new_size) {
uint8_t *tmp = av_realloc_array(array, new_size, elem_size);
uint8_t *tmp = av_realloc_array(*array, new_size, elem_size);
if (!tmp)
report_and_exit(AVERROR(ENOMEM));
return AVERROR(ENOMEM);
memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
*size = new_size;
return tmp;
*array = tmp;
return 0;
}
return array;
return 0;
}
void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)

View File

@ -416,15 +416,15 @@ FILE *get_preset_file(char *filename, size_t filename_size,
/**
* Realloc array to hold new_size elements of elem_size.
* Calls exit() on failure.
*
* @param array array to reallocate
* @param array pointer to the array to reallocate, will be updated
* with a new pointer on success
* @param elem_size size in bytes of each element
* @param size new element count will be written here
* @param new_size number of elements to place in reallocated array
* @return reallocated array
* @return a non-negative number on success, a negative error code on failure
*/
void *grow_array(void *array, int elem_size, int *size, int new_size);
int 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
@ -440,7 +440,11 @@ void *grow_array(void *array, int elem_size, int *size, int new_size);
void *allocate_array_elem(void *array, size_t elem_size, int *nb_elems);
#define GROW_ARRAY(array, nb_elems)\
array = grow_array(array, sizeof(*array), &nb_elems, nb_elems + 1)
do { \
int _ret = grow_array((void**)&array, sizeof(*array), &nb_elems, nb_elems + 1); \
if (_ret < 0) \
report_and_exit(_ret); \
} while (0)
#define GET_PIX_FMT_NAME(pix_fmt)\
const char *name = av_get_pix_fmt_name(pix_fmt);