From: Jiasheng Jiang <jiashengjiangcool@gmail.com> To: ffmpeg-devel@ffmpeg.org Cc: Jiasheng Jiang <jiashengjiangcool@gmail.com> Subject: [FFmpeg-devel] [PATCH v2] tests/api/api-h264-test: Add proper error handling to avoid potential memory leaks Date: Mon, 11 Aug 2025 21:10:21 +0000 Message-ID: <20250811211021.34174-1-jiashengjiangcool@gmail.com> (raw) Add proper error handling to free allocated memory to avoid potential memory leaks. Fixes: ca3b274552 ("api-h264-test: build with another api test") Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com> --- Changelog: v1 -> v2: 1. Fix the goto labels. --- tests/api/api-h264-test.c | 49 +++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/tests/api/api-h264-test.c b/tests/api/api-h264-test.c index e1e8803c1e..4a8324c789 100644 --- a/tests/api/api-h264-test.c +++ b/tests/api/api-h264-test.c @@ -55,13 +55,14 @@ static int video_decode_example(const char *input_filename) result = avformat_find_stream_info(fmt_ctx, NULL); if (result < 0) { av_log(NULL, AV_LOG_ERROR, "Can't get stream info\n"); - return result; + goto close_fmt_ctx; } video_stream = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); if (video_stream < 0) { - av_log(NULL, AV_LOG_ERROR, "Can't find video stream in input file\n"); - return -1; + av_log(NULL, AV_LOG_ERROR, "Can't find video stream in input file\n"); + result = -1; + goto close_fmt_ctx; } origin_par = fmt_ctx->streams[video_stream]->codecpar; @@ -69,44 +70,49 @@ static int video_decode_example(const char *input_filename) codec = avcodec_find_decoder(origin_par->codec_id); if (!codec) { av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n"); - return -1; + result = -1; + goto close_fmt_ctx; } ctx = avcodec_alloc_context3(codec); if (!ctx) { av_log(NULL, AV_LOG_ERROR, "Can't allocate decoder context\n"); - return AVERROR(ENOMEM); + result = AVERROR(ENOMEM); + goto close_fmt_ctx; } result = avcodec_parameters_to_context(ctx, origin_par); if (result) { av_log(NULL, AV_LOG_ERROR, "Can't copy decoder context\n"); - return result; + goto free_ctx; } result = avcodec_open2(ctx, codec, NULL); if (result < 0) { av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n"); - return result; + goto free_ctx; } fr = av_frame_alloc(); if (!fr) { av_log(NULL, AV_LOG_ERROR, "Can't allocate frame\n"); - return AVERROR(ENOMEM); + result = AVERROR(ENOMEM); + goto free_ctx; } pkt = av_packet_alloc(); if (!pkt) { av_log(NULL, AV_LOG_ERROR, "Cannot allocate packet\n"); - return AVERROR(ENOMEM); + result = AVERROR(ENOMEM); + goto free_fr; } byte_buffer_size = av_image_get_buffer_size(ctx->pix_fmt, ctx->width, ctx->height, 16); byte_buffer = av_malloc(byte_buffer_size); if (!byte_buffer) { av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n"); - return AVERROR(ENOMEM); + result = AVERROR(ENOMEM); + goto free_pkt; } printf("#tb %d: %d/%d\n", video_stream, fmt_ctx->streams[video_stream]->time_base.num, fmt_ctx->streams[video_stream]->time_base.den); @@ -131,19 +137,19 @@ static int video_decode_example(const char *input_filename) if (result < 0) { av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n"); - return result; + goto free_byte_buffer; } while (result >= 0) { result = avcodec_receive_frame(ctx, fr); if (result == AVERROR_EOF) - goto finish; + goto free_byte_buffer; else if (result == AVERROR(EAGAIN)) { result = 0; break; } else if (result < 0) { av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n"); - return result; + goto free_byte_buffer; } number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size, @@ -152,7 +158,8 @@ static int video_decode_example(const char *input_filename) if (number_of_written_bytes < 0) { av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n"); av_frame_unref(fr); - return number_of_written_bytes; + result = number_of_written_bytes; + goto free_byte_buffer; } printf("%d, %s, %s, %8"PRId64", %8d, 0x%08"PRIx32"\n", video_stream, av_ts2str(fr->pts), av_ts2str(fr->pkt_dts), fr->duration, @@ -163,13 +170,19 @@ static int video_decode_example(const char *input_filename) i++; } -finish: + result = 0; + +free_byte_buffer: + av_freep(&byte_buffer); +free_pkt: av_packet_free(&pkt); +free_fr: av_frame_free(&fr); - avformat_close_input(&fmt_ctx); +free_ctx: avcodec_free_context(&ctx); - av_freep(&byte_buffer); - return 0; +close_fmt_ctx: + avformat_close_input(&fmt_ctx); + return result; } int main(int argc, char **argv) -- 2.25.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".
reply other threads:[~2025-08-11 21:10 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20250811211021.34174-1-jiashengjiangcool@gmail.com \ --to=jiashengjiangcool@gmail.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git