Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] tests/api/api-h264-test: Add proper error handling to avoid potential memory leaks
@ 2025-08-08 19:22 Jiasheng Jiang
  2025-08-11 20:55 ` Michael Niedermayer
  0 siblings, 1 reply; 2+ messages in thread
From: Jiasheng Jiang @ 2025-08-08 19:22 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Jiasheng Jiang

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>
---
 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..25b91a3630 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 cloce_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 cloce_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 cloce_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 free_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;
+cloce_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".

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [FFmpeg-devel] [PATCH] tests/api/api-h264-test: Add proper error handling to avoid potential memory leaks
  2025-08-08 19:22 [FFmpeg-devel] [PATCH] tests/api/api-h264-test: Add proper error handling to avoid potential memory leaks Jiasheng Jiang
@ 2025-08-11 20:55 ` Michael Niedermayer
  0 siblings, 0 replies; 2+ messages in thread
From: Michael Niedermayer @ 2025-08-11 20:55 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


[-- Attachment #1.1: Type: text/plain, Size: 1163 bytes --]

On Fri, Aug 08, 2025 at 07:22:29PM +0000, Jiasheng Jiang wrote:
[...]
> @@ -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 cloce_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 free_fmt_ctx;


make -j32  fate-api-h264
CC	tests/api/api-h264-test.o
tests/api/api-h264-test.c: In function ‘video_decode_example’:
tests/api/api-h264-test.c:81:9: error: label ‘free_fmt_ctx’ used but not defined
   81 |         goto free_fmt_ctx;
      |         ^~~~
make: *** [ffbuild/common.mak:81: tests/api/api-h264-test.o] Error 1

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Nations do behave wisely once they have exhausted all other alternatives. 
-- Abba Eban

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

[-- Attachment #2: Type: text/plain, Size: 251 bytes --]

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-08-11 20:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-08-08 19:22 [FFmpeg-devel] [PATCH] tests/api/api-h264-test: Add proper error handling to avoid potential memory leaks Jiasheng Jiang
2025-08-11 20:55 ` Michael Niedermayer

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