From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 89C7D46361 for ; Sat, 15 Jul 2023 10:51:04 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 87DE568C76C; Sat, 15 Jul 2023 13:47:09 +0300 (EEST) Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 81FE668C4F8 for ; Sat, 15 Jul 2023 13:46:46 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 380E52404EE for ; Sat, 15 Jul 2023 12:46:46 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id tYL1-mU1QfA1 for ; Sat, 15 Jul 2023 12:46:45 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 1B50F240D2A for ; Sat, 15 Jul 2023 12:46:27 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 38C373A2125 for ; Sat, 15 Jul 2023 12:46:21 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 15 Jul 2023 12:45:51 +0200 Message-Id: <20230715104611.17902-27-anton@khirnov.net> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230715104611.17902-1-anton@khirnov.net> References: <20230715104611.17902-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 27/47] fftools/cmdutils: add error handling to grow_array() X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: --- fftools/cmdutils.c | 19 ++++++++++++------- fftools/cmdutils.h | 14 +++++++++----- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c index 6c627ee815..63b29c7a3a 100644 --- a/fftools/cmdutils.c +++ b/fftools/cmdutils.c @@ -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) diff --git a/fftools/cmdutils.h b/fftools/cmdutils.h index 6b9d7f80ae..0dfe8b313c 100644 --- a/fftools/cmdutils.h +++ b/fftools/cmdutils.h @@ -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); -- 2.40.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".