From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id ED84C4CC98 for ; Fri, 8 Aug 2025 18:23:55 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id 4854F68CA40; Fri, 8 Aug 2025 21:23:52 +0300 (EEST) Received: from mail-il1-f182.google.com (mail-il1-f182.google.com [209.85.166.182]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id 394EC687BF2 for ; Fri, 8 Aug 2025 21:23:46 +0300 (EEST) Received: by mail-il1-f182.google.com with SMTP id e9e14a558f8ab-3e40ac40940so10651535ab.0 for ; Fri, 08 Aug 2025 11:23:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754677424; x=1755282224; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=X7IP7eXu/YHaeAEq/E7DDoHXdvZ53foZll5ypYb+sfg=; b=TP/9H/M4465NBea8nuFj8TzvBO0ByUP8fowb8ZR/Fb/TInHJcxCW8RQmMFXC2TAULB +zb1Zz5MwlvuUftArHr+Z644r7jCpXa2cU4oWR2b3208Jn0ngS/INjfmaSJtL3tCGftw ctMF9KjxSPaPnD5GcTu01jtpzUaM1/uqIHdFekCHDvcQ+sP5VvqgZn0dLGgRJEgbD+Gy E3A3G6/TcIse4nkOruJ80tVb0Jd24aLT9CNmXvD6D4qWKGH7zm/j7rg2eCUEVVamzlgd l632+ypZgNNZJkO71noMBczfW1XNrXcoCU883Pc/t3M84BA1+uKtIpPcFq7i/6Lhg9ns iPhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754677424; x=1755282224; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=X7IP7eXu/YHaeAEq/E7DDoHXdvZ53foZll5ypYb+sfg=; b=bjqIZQulMcC1xUQbXsUc/hWdaYt+R/heluaLz4Nt8GQGfPKw35YqxGP0nLiGLU+FZc Aj2qqUu1mp6gT8Yl3+sE+k3FE6KhPC1PY6sHIR7rF3HZD3r8Xs6BNNvbuYtICJD0V73g TU5KmbH+XKzNCtL9kw/et2WFBcVKlYr9kDnkgZ8ZiV9BbaBTwAyS2OuYIxQ1MhHlk2eq iZ1YdUf4gUKoQTyPMWFUO6PDidKOgg3G4mIBjPi37sZdRSugBlq1Sl7ovedHGx1ZxDrX yUppl2uveoVMsqRtlnEp4VHTq2yRT8ErEdrtctn8AWS8Sfo6iztxKIhBOSIfSlTSH+pK qauQ== X-Gm-Message-State: AOJu0Yz33fpNfK5xNOC+1SnCHosNTopxS1NdedC6RA9pV7Q9Tcc/6fuC +0rM+alHPzFEwTyCPqdrvOOA+T2tYpSf4dWt6lCVdxDOBZDqTGZ2P6vVNExpbw== X-Gm-Gg: ASbGnctbacbm2FRlBLq0XnMx+HSQvHcnC5yVuZDGVS3s/Y40bIxMxYTbAsf9tm0QsOh hmPHn9S0NgJm05Xs1ulj4JY2BJUrd+O+8mvnPLmcfnnX1jdYXirdRVI0UNQxQJe4uRijAlleqH6 tqpZG06qOIHFWqbwXS7UuoMXIfVHrDGp/yQcaEMpGXYAYJsyjXpEmrgkmxcpvDLAehtqRW8zGFI rI5XzJkGC/xdPTmTGZ5g+HCehb1CZUaEKuoH3BTsE1kO18yctwvk74leEkzVHD3xUO3pELsQv+x LfFmRcW3Ffhhs9SeoLdQZBh25kF8mu7PA9QOL4GGlZk+J56koTsuagXE8q+XXdKNJ78V1nOUGwp JR8Mfc1S5rUHX8opK+Mp+BwyeUmVBnFcqkwivsdWvRapf X-Google-Smtp-Source: AGHT+IEkA6TkQOIUobGFKCEsLP9aCtNjqlzcR2zJFEr97s3J3E6ZNBj3aRsVgoLDqYLzOrRswq2iQQ== X-Received: by 2002:a05:6e02:228c:b0:3e3:d822:f180 with SMTP id e9e14a558f8ab-3e53384911emr57362225ab.0.1754677423989; Fri, 08 Aug 2025 11:23:43 -0700 (PDT) Received: from newman.cs.purdue.edu ([128.10.127.250]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-3e533ccf6ddsm9248085ab.42.2025.08.08.11.23.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Aug 2025 11:23:43 -0700 (PDT) From: Jiasheng Jiang To: ffmpeg-devel@ffmpeg.org Date: Fri, 8 Aug 2025 18:23:41 +0000 Message-Id: <20250808182341.2855-1-jiashengjiangcool@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] tests/api/api-band-test: Add proper error handling to avoid potential memory leaks 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 Cc: Jiasheng Jiang 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: 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 --- 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".