>From 78bb1086f99b6ba234a9874246feb15360c8496c Mon Sep 17 00:00:00 2001 From: OxBat Date: Sat, 3 Jan 2026 19:23:35 +0100 Subject: [PATCH] examples: fix memory leaks, zombie processes and integer overflows --- doc/examples/avio_http_serve_files.c | 2 ++ doc/examples/extract_mvs.c | 3 ++- doc/examples/filter_audio.c | 6 ++++++ doc/examples/hw_decode.c | 6 ++++-- doc/examples/qsv_transcode.c | 5 +++++ doc/examples/transcode.c | 14 ++++++++------ 6 files changed, 27 insertions(+), 9 deletions(-) diff --git a/doc/examples/avio_http_serve_files.c b/doc/examples/avio_http_serve_files.c index 2aae3870c2..9aaf4b273b 100644 --- a/doc/examples/avio_http_serve_files.c +++ b/doc/examples/avio_http_serve_files.c @@ -31,6 +31,7 @@ #include #include #include +#include /* NEW */ static void process_client(AVIOContext *client, const char *in_uri) { @@ -103,6 +104,7 @@ int main(int argc, char **argv) const char *in_uri, *out_uri; int ret, pid; av_log_set_level(AV_LOG_TRACE); + signal(SIGCHLD, SIG_IGN); /* PATCH: Prevent zombie processes */ if (argc < 3) { printf("usage: %s input http://hostname[:port]\n" "API example program to serve http to multiple clients.\n" diff --git a/doc/examples/extract_mvs.c b/doc/examples/extract_mvs.c index 5603064d72..3052473e81 100644 --- a/doc/examples/extract_mvs.c +++ b/doc/examples/extract_mvs.c @@ -105,10 +105,11 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type) fprintf(stderr, "Failed to allocate codec\n"); return AVERROR(EINVAL); } - + /* PATCH: Memory leak*/ ret = avcodec_parameters_to_context(dec_ctx, st->codecpar); if (ret < 0) { fprintf(stderr, "Failed to copy codec parameters to codec context\n"); + avcodec_free_context(&dec_ctx); /* PATCH: Fix leak */ return ret; } diff --git a/doc/examples/filter_audio.c b/doc/examples/filter_audio.c index ad77bf1f89..c4673bddef 100644 --- a/doc/examples/filter_audio.c +++ b/doc/examples/filter_audio.c @@ -88,6 +88,7 @@ static int init_filter_graph(AVFilterGraph **graph, AVFilterContext **src, abuffer = avfilter_get_by_name("abuffer"); if (!abuffer) { fprintf(stderr, "Could not find the abuffer filter.\n"); + avfilter_graph_free(&filter_graph); /* PATCH: Fix leak */ return AVERROR_FILTER_NOT_FOUND; } @@ -279,6 +280,11 @@ int main(int argc, char *argv[]) } duration = atof(argv[1]); +/* PATCH: Check for integer overflow */ + if (duration > (double)2147483647 / INPUT_SAMPLERATE) { + fprintf(stderr, "Duration too long\n"); + return 1; + } nb_frames = duration * INPUT_SAMPLERATE / FRAME_SIZE; if (nb_frames <= 0) { fprintf(stderr, "Invalid duration: %s\n", argv[1]); diff --git a/doc/examples/hw_decode.c b/doc/examples/hw_decode.c index ac4e445505..caef3e93e3 100644 --- a/doc/examples/hw_decode.c +++ b/doc/examples/hw_decode.c @@ -215,8 +215,10 @@ int main(int argc, char *argv[]) return AVERROR(ENOMEM); video = input_ctx->streams[video_stream]; - if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0) - return -1; + if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0) { + avcodec_free_context(&decoder_ctx); /* PATCH: Fix leak */ + return -1; +} decoder_ctx->get_format = get_hw_format; diff --git a/doc/examples/qsv_transcode.c b/doc/examples/qsv_transcode.c index 13b4933041..b780a65c79 100644 --- a/doc/examples/qsv_transcode.c +++ b/doc/examples/qsv_transcode.c @@ -351,6 +351,11 @@ int main(int argc, char **argv) } setting_number = (argc - 5) / 2; dynamic_setting = av_malloc(setting_number * sizeof(*dynamic_setting)); + /* PATCH: Check allocation */ + if (!dynamic_setting) { + ret = AVERROR(ENOMEM); + goto end; + } current_setting_number = 0; for (int i = 0; i < setting_number; i++) { dynamic_setting[i].frame_number = atoi(argv[i*2 + 5]); diff --git a/doc/examples/transcode.c b/doc/examples/transcode.c index 1dc1b50502..6dcacc1d4f 100644 --- a/doc/examples/transcode.c +++ b/doc/examples/transcode.c @@ -92,12 +92,14 @@ static int open_input_file(const char *filename) av_log(NULL, AV_LOG_ERROR, "Failed to allocate the decoder context for stream #%u\n", i); return AVERROR(ENOMEM); } - ret = avcodec_parameters_to_context(codec_ctx, stream->codecpar); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Failed to copy decoder parameters to input decoder context " - "for stream #%u\n", i); - return ret; - } + /* PATCH: Memory leak - Input Decoder */ + ret = avcodec_parameters_to_context(codec_ctx, stream->codecpar); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Failed to copy decoder parameters to input decoder context " + "for stream #%u\n", i); + avcodec_free_context(&codec_ctx); /* PATCH: Fix leak */ + return ret; + } /* Inform the decoder about the timebase for the packet timestamps. * This is highly recommended, but not mandatory. */ -- 2.52.0.windows.1