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 D8A4B4CCA2 for ; Fri, 8 Aug 2025 18:39:15 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id C365A68CA5E; Fri, 8 Aug 2025 21:39:11 +0300 (EEST) Received: from mail-io1-f48.google.com (mail-io1-f48.google.com [209.85.166.48]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id D894C687ACC for ; Fri, 8 Aug 2025 21:39:05 +0300 (EEST) Received: by mail-io1-f48.google.com with SMTP id ca18e2360f4ac-8817b8d2d0eso83163039f.0 for ; Fri, 08 Aug 2025 11:39:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754678344; x=1755283144; 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=CQI32Rrv2wCPLzNN54nbh8kaaHjBLOgzil0XDih6dHw=; b=Mdv2AyHG54gGyOBioF7/NsAwQeG8SmRLeYupBfmyaiilz9+zHb1wyiXT36A10gA303 pT0J1KJXQDJTb2NsdT8WqlpGVY5wexWEZUxgrv7CyHGbYW+xM9fImwJR+1GW0NXz5sz2 d9uZ4GvnWJHB8mHay5ynyuMVzFPjkttSMnxC45SCX6GtSbpfO4VlsEoCqRGoUK+SJE2C aGQ/WYmeWrn+Ot2CrrdlXKKAvwPmn5tK/IMYK4ksnVF40AZdP1UgI5ZhwggRGN53IW74 QC1YSD0HKzeNNoh1z3ID7IB73/L4EUBKfAkrhYBDxRRdv1LQCyAaO5V32KffkpIhaao5 jHxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754678344; x=1755283144; 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=CQI32Rrv2wCPLzNN54nbh8kaaHjBLOgzil0XDih6dHw=; b=T3MZYQE7mrE2UNv680Fxdo7MeHLU8drhCCmxpeqm0A9HEKbuxVzvKZQFC9LLQthI42 hHcikdu6vXKyvQA+oOnfwUQr41tyGemzGru/hlHGQfUiMY8o+CIR3Jafg+VDsNixKL48 B+GmQxKk3CZTo90mbmT9h7t8V9h9CYddM1hPwPxDdEgLLlVutsGHHs3PmpJmRo+/uIiN OTR8hTHSA1lVUOdaH2uDax7yTONPuLqb8ThndPQbfcsvBcJsAj0/PT6vKcz/78iIAlRP BRlfzi+TGNZ/iasTRL7McSmd4CLR7HzJWIMc5OLolJbdi16KtXpyhGrpsWo6W8wmgwpC gxJg== X-Gm-Message-State: AOJu0YwQpMrsFQFZrvKbV+KhHMzcMq4hlQXwf6Chf0omPQ5PTds4eWdM V8THXNnngVFTefeJwrDCTWW4x+H9i59GjNjVPGwuTv33E/eTsKzzdldmqKKd0A== X-Gm-Gg: ASbGncu2jen8IqvtaI98W2oglAl24jpAkIDPb8v5n8TP8MJUDm5LpdbOyhIOcNnZtuo URhWKqYaOsUVOAwzmMjRpgv4CkMaJpFjccy7lEW9elwoEziNFP2D80qWvkYELx8QoJncnS+g4KH +KiO/8nC7FIy0Qqh/IylD4uFgdoIFPj8itQvRlwIqfhrD3FkNZNC0VbuP+AD0A+ArkbRCTXTXrL Pj3FYE/IvN1ZH38hF+5CPcO4mmG01nIrQdckrmeZDkvPhMrqCdJfbtL5GbErdxfrtiFcpaytOhk YbInuBhxuHurLxV3j+OL+sUX8+fRpAx1Sqc0Bza11+BndF9KfQg6ASYwEFG/raYNeoyfLv3bSdT UNOEZBkwuZLHD/Txhqp0niXhpWow33T29lYF7pdDzpzeP X-Google-Smtp-Source: AGHT+IE+BBOtDXsScH8Ox/GTSf6X+4wIdPe6o4jRb6ylQOtDTlVlJ1qJOfa+DzorbQe4V0I3Tk0TYg== X-Received: by 2002:a05:6e02:e44:b0:3e5:3897:b11e with SMTP id e9e14a558f8ab-3e53897b314mr24959325ab.14.1754678343635; Fri, 08 Aug 2025 11:39:03 -0700 (PDT) Received: from newman.cs.purdue.edu ([128.10.127.250]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-50ae99cd74csm628117173.25.2025.08.08.11.39.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Aug 2025 11:39:03 -0700 (PDT) From: Jiasheng Jiang To: ffmpeg-devel@ffmpeg.org Date: Fri, 8 Aug 2025 18:39:01 +0000 Message-Id: <20250808183901.4011-1-jiashengjiangcool@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] tests/api/api-flac-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: be70c79af2 ("libavcodec: Add FLAC API test") Signed-off-by: Jiasheng Jiang --- tests/api/api-flac-test.c | 61 +++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/tests/api/api-flac-test.c b/tests/api/api-flac-test.c index e65e261b98..56f5108149 100644 --- a/tests/api/api-flac-test.c +++ b/tests/api/api-flac-test.c @@ -72,6 +72,7 @@ static int init_encoder(const AVCodec *enc, AVCodecContext **enc_ctx, result = avcodec_open2(ctx, enc, NULL); if (result < 0) { + avcodec_free_context(&ctx); av_log(ctx, AV_LOG_ERROR, "Can't open encoder\n"); return result; } @@ -97,6 +98,7 @@ static int init_decoder(const AVCodec *dec, AVCodecContext **dec_ctx, result = avcodec_open2(ctx, dec, NULL); if (result < 0) { + avcodec_free_context(&ctx); av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n"); return result; } @@ -125,55 +127,61 @@ static int run_test(const AVCodec *enc, const AVCodec *dec, in_frame = av_frame_alloc(); if (!in_frame) { av_log(NULL, AV_LOG_ERROR, "Can't allocate input frame\n"); - return AVERROR(ENOMEM); + result = AVERROR(ENOMEM); + goto free_enc_pkt; } in_frame->nb_samples = enc_ctx->frame_size; in_frame->format = enc_ctx->sample_fmt; result = av_channel_layout_copy(&in_frame->ch_layout, &enc_ctx->ch_layout); if (result < 0) - return result; + goto free_in_frame; if (av_frame_get_buffer(in_frame, 0) != 0) { av_log(NULL, AV_LOG_ERROR, "Can't allocate a buffer for input frame\n"); - return AVERROR(ENOMEM); + result = AVERROR(ENOMEM); + goto free_in_frame; } out_frame = av_frame_alloc(); if (!out_frame) { av_log(NULL, AV_LOG_ERROR, "Can't allocate output frame\n"); - return AVERROR(ENOMEM); + result = AVERROR(ENOMEM); + goto free_in_frame; } raw_in = av_malloc(in_frame->linesize[0] * NUMBER_OF_AUDIO_FRAMES); if (!raw_in) { av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for raw_in\n"); - return AVERROR(ENOMEM); + result = AVERROR(ENOMEM); + goto free_out_frame; } raw_out = av_malloc(in_frame->linesize[0] * NUMBER_OF_AUDIO_FRAMES); if (!raw_out) { av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for raw_out\n"); - return AVERROR(ENOMEM); + result = AVERROR(ENOMEM); + goto free_raw_in; } for (i = 0; i < NUMBER_OF_AUDIO_FRAMES; i++) { result = av_frame_make_writable(in_frame); if (result < 0) - return result; + goto free_raw_out; generate_raw_frame((uint16_t*)(in_frame->data[0]), i, enc_ctx->sample_rate, enc_ctx->ch_layout.nb_channels, enc_ctx->frame_size); in_frame_bytes = in_frame->nb_samples * in_frame->ch_layout.nb_channels * sizeof(uint16_t); if (in_frame_bytes > in_frame->linesize[0]) { av_log(NULL, AV_LOG_ERROR, "Incorrect value of input frame linesize\n"); - return 1; + result = 1; + goto free_raw_out; } memcpy(raw_in + in_offset, in_frame->data[0], in_frame_bytes); in_offset += in_frame_bytes; result = avcodec_send_frame(enc_ctx, in_frame); if (result < 0) { av_log(NULL, AV_LOG_ERROR, "Error submitting a frame for encoding\n"); - return result; + goto free_raw_out; } while (result >= 0) { @@ -182,7 +190,7 @@ static int run_test(const AVCodec *enc, const AVCodec *dec, break; else if (result < 0 && result != AVERROR_EOF) { av_log(NULL, AV_LOG_ERROR, "Error encoding audio frame\n"); - return result; + goto free_raw_out; } /* if we get an encoded packet, feed it straight to the decoder */ @@ -190,7 +198,7 @@ static int run_test(const AVCodec *enc, const AVCodec *dec, av_packet_unref(enc_pkt); if (result < 0) { av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n"); - return result; + goto free_raw_out; } result = avcodec_receive_frame(dec_ctx, out_frame); @@ -202,27 +210,31 @@ static int run_test(const AVCodec *enc, const AVCodec *dec, break; } else if (result < 0) { av_log(NULL, AV_LOG_ERROR, "Error decoding audio packet\n"); - return result; + goto free_raw_out; } if (in_frame->nb_samples != out_frame->nb_samples) { av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different number of samples\n"); - return AVERROR_UNKNOWN; + result = AVERROR_UNKNOWN; + goto free_raw_out; } if (av_channel_layout_compare(&in_frame->ch_layout, &out_frame->ch_layout)) { av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different channel layout\n"); - return AVERROR_UNKNOWN; + result = AVERROR_UNKNOWN; + goto free_raw_out; } if (in_frame->format != out_frame->format) { av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different sample format\n"); - return AVERROR_UNKNOWN; + result = AVERROR_UNKNOWN; + goto free_raw_out; } out_frame_bytes = out_frame->nb_samples * out_frame->ch_layout.nb_channels * sizeof(uint16_t); if (out_frame_bytes > out_frame->linesize[0]) { av_log(NULL, AV_LOG_ERROR, "Incorrect value of output frame linesize\n"); - return 1; + result = 1; + goto free_raw_out; } memcpy(raw_out + out_offset, out_frame->data[0], out_frame_bytes); out_offset += out_frame_bytes; @@ -231,17 +243,24 @@ static int run_test(const AVCodec *enc, const AVCodec *dec, if (memcmp(raw_in, raw_out, out_frame_bytes * NUMBER_OF_AUDIO_FRAMES) != 0) { av_log(NULL, AV_LOG_ERROR, "Output differs\n"); - return 1; + result = 1; + goto free_raw_out; } av_log(NULL, AV_LOG_INFO, "OK\n"); + result = 0; - av_freep(&raw_in); +free_raw_out: av_freep(&raw_out); - av_packet_free(&enc_pkt); - av_frame_free(&in_frame); +free_raw_in: + av_freep(&raw_in); +free_out_frame: av_frame_free(&out_frame); - return 0; +free_in_frame: + av_frame_free(&in_frame); +free_enc_pkt: + av_packet_free(&enc_pkt); + return result; } int main(void) -- 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".