Commit c37079067e for ffmpeg
commit c37079067e160a165f7f3b3ce93ce6c480b0d700
Author: Kacper Michajłow <kasper93@gmail.com>
Date: Wed Oct 14 10:16:49 2026 +0200
avformat: skip the program lookup in av_new_program() for monotonic ids
Track whether the program ids are strictly increasing, like
avpriv_new_chapter() does, and skip the scan of the existing programs
when the new id is above the last one.
Fixes: Timeout (7s -> 240ms)
Fixes: clusterfuzz-testcase-ffmpeg_dem_HLS_fuzzer-5202060644253696
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
diff --git a/libavformat/avformat.c b/libavformat/avformat.c
index 17c0534de2..e8ebb89097 100644
--- a/libavformat/avformat.c
+++ b/libavformat/avformat.c
@@ -296,14 +296,21 @@ const char *avformat_stream_group_name(enum AVStreamGroupParamsType type)
AVProgram *av_new_program(AVFormatContext *ac, int id)
{
+ FFFormatContext *const si = ffformatcontext(ac);
AVProgram *program = NULL;
int ret;
av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id);
- for (unsigned i = 0; i < ac->nb_programs; i++)
- if (ac->programs[i]->id == id)
- program = ac->programs[i];
+ if (!ac->nb_programs) {
+ si->program_ids_monotonic = 1;
+ } else if (!si->program_ids_monotonic || ac->programs[ac->nb_programs - 1]->id >= id) {
+ for (unsigned i = 0; i < ac->nb_programs; i++)
+ if (ac->programs[i]->id == id)
+ program = ac->programs[i];
+ if (!program)
+ si->program_ids_monotonic = 0;
+ }
if (!program) {
program = av_mallocz(sizeof(*program));
diff --git a/libavformat/internal.h b/libavformat/internal.h
index b77292a532..53dc7b939b 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -119,6 +119,11 @@ typedef struct FFFormatContext {
int missing_streams;
+ /**
+ * Set if program ids are strictly monotonic.
+ */
+ int program_ids_monotonic;
+
/**
* Shared libcurl event loop, created on demand on the first use. Freed on
* context free. This allows to share libcurl state across URLContexts,