From: Jiasheng Jiang <jiashengjiangcool@gmail.com> To: ffmpeg-devel@ffmpeg.org Cc: Jiasheng Jiang <jiashengjiangcool@gmail.com> Subject: [FFmpeg-devel] [PATCH] tests/api/api-band-test: Add proper error handling to avoid potential memory leaks Date: Fri, 8 Aug 2025 18:23:41 +0000 Message-ID: <20250808182341.2855-1-jiashengjiangcool@gmail.com> (raw) Add proper error handling to free allocated memory to avoid potential memory leaks. Fixes: 4a9bc12fe7 ("fate: add api-band-test") Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com> --- tests/api/api-band-test.c | 62 +++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/tests/api/api-band-test.c b/tests/api/api-band-test.c index c3612bedb9..55fc31dff7 100644 --- a/tests/api/api-band-test.c +++ b/tests/api/api-band-test.c @@ -89,13 +89,14 @@ static int video_decode(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_input; } 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; + result = -1; + goto close_input; } origin_par = fmt_ctx->streams[video_stream]->codecpar; @@ -103,24 +104,27 @@ static int video_decode(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_input; } if (!(codec->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)) { av_log(NULL, AV_LOG_ERROR, "Codec does not support draw_horiz_band\n"); - return -1; + result = -1; + goto close_input; } 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_input; } 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; } ctx->draw_horiz_band = draw_horiz_band; @@ -129,32 +133,36 @@ static int video_decode(const char *input_filename) 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, 32); 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; } slice_byte_buffer = av_malloc(byte_buffer_size); if (!slice_byte_buffer) { av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n"); - return AVERROR(ENOMEM); + result = AVERROR(ENOMEM); + goto free_byte_buffer; } memset(slice_byte_buffer, 0, byte_buffer_size); @@ -173,19 +181,19 @@ static int video_decode(const char *input_filename) if (result < 0) { av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n"); - return result; + goto free_slice_byte_buffer; } while (result >= 0) { result = avcodec_receive_frame(ctx, fr); if (result == AVERROR_EOF) - goto finish; + goto free_slice_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_slice_byte_buffer; } number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size, @@ -193,29 +201,39 @@ static int video_decode(const char *input_filename) ctx->pix_fmt, ctx->width, ctx->height, 1); if (number_of_written_bytes < 0) { av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n"); - return number_of_written_bytes; + result = number_of_written_bytes; + goto free_slice_byte_buffer; } if (draw_horiz_band_called == 0) { av_log(NULL, AV_LOG_ERROR, "draw_horiz_band haven't been called!\n"); - return -1; + result = -1; + goto free_slice_byte_buffer; } if (av_adler32_update(0, (const uint8_t*)byte_buffer, number_of_written_bytes) != av_adler32_update(0, (const uint8_t*)slice_byte_buffer, number_of_written_bytes)) { av_log(NULL, AV_LOG_ERROR, "Decoded frames with and without draw_horiz_band are not the same!\n"); - return -1; + result = -1; + goto free_slice_byte_buffer; } av_frame_unref(fr); } } -finish: + result = 0; + +free_slice_byte_buffer: + av_freep(&slice_byte_buffer); +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); - av_freep(&slice_byte_buffer); - return 0; +close_input: + 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-08 18:23 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=20250808182341.2855-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