PR #25960 opened by Jun Zhao (mypopydev) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25960 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25960.patch
5a75d905cb24 added stream_groups[MAX_STREAMS_PER_PROGRAM] to struct Program, making it 134688 bytes on 64-bit targets. Two callbacks keep a whole struct Program on the stack: FFSWAP() in pat_cb() creates a by-value temporary, and pmt_cb() has a local struct Program old_program. GCC -fstack-usage reports 134800 bytes for pat_cb() and 136368 bytes for pmt_cb(). The ffmpeg CLI demuxes each input in a thread created by task_start() with default attributes; musl's default thread stack is 131072 bytes. Both callbacks overflow it as soon as a PAT or PMT is parsed in that thread, so ffmpeg dies with SIGSEGV on every MPEG-TS input. This is not musl specific: any thread with a stack below ~140 KiB is affected. Swap the two programs in 1 KiB chunks in a helper instead of creating a full struct Program temporary. A plain assignment is not equivalent: pat_cb() compacts the active programs while ts->nb_prg still holds the old value, so all old entries must remain findable. pmt_cb() only needs the old program's stream list, which find_matching_stream() reads through nb_streams and streams[]. Save just the active entries in a new struct ProgramStreams (1028 bytes) before clear_program(prg) and the PMT loop overwrite prg->streams[] in place. Measured with -fstack-usage on arm64: pat_cb() 134848 -> 1200 bytes, pmt_cb() 136400 -> 2752 bytes, both now below configure's -Wstack-usage=122880 threshold. Fixes: https://code.ffmpeg.org/FFmpeg/FFmpeg/issues/25937 Signed-off-by: Jun Zhao <[email protected]> # Summary of changes Briefly describe what this PR does and why. <!-- If this PR requires new FATE test samples, attach them to the PR and list their target paths below (relative to the fate-suite root). Attached filenames must match the sample's filename: ```fate-samples # e.g. vorbis/new-sample.ogg ``` --> >From 9d6114c45a7c0c4c1d4de719e75e45c348baa83d Mon Sep 17 00:00:00 2001 From: Jun Zhao <[email protected]> Date: Thu, 12 Nov 2026 07:33:32 +0800 Subject: [PATCH] avformat/mpegts: avoid ~131 KiB stack frames in PAT/PMT callbacks 5a75d905cb24 added stream_groups[MAX_STREAMS_PER_PROGRAM] to struct Program, making it 134688 bytes on 64-bit targets. Two callbacks keep a whole struct Program on the stack: FFSWAP() in pat_cb() creates a by-value temporary, and pmt_cb() has a local struct Program old_program. GCC -fstack-usage reports 134800 bytes for pat_cb() and 136368 bytes for pmt_cb(). The ffmpeg CLI demuxes each input in a thread created by task_start() with default attributes; musl's default thread stack is 131072 bytes. Both callbacks overflow it as soon as a PAT or PMT is parsed in that thread, so ffmpeg dies with SIGSEGV on every MPEG-TS input. This is not musl specific: any thread with a stack below ~140 KiB is affected. Swap the two programs in 1 KiB chunks in a helper instead of creating a full struct Program temporary. A plain assignment is not equivalent: pat_cb() compacts the active programs while ts->nb_prg still holds the old value, so all old entries must remain findable. pmt_cb() only needs the old program's stream list, which find_matching_stream() reads through nb_streams and streams[]. Save just the active entries in a new struct ProgramStreams (1028 bytes) before clear_program(prg) and the PMT loop overwrite prg->streams[] in place. Measured with -fstack-usage on arm64: pat_cb() 134848 -> 1200 bytes, pmt_cb() 136400 -> 2752 bytes, both now below configure's -Wstack-usage=122880 threshold. Fixes: https://code.ffmpeg.org/FFmpeg/FFmpeg/issues/25937 Signed-off-by: Jun Zhao <[email protected]> --- libavformat/mpegts.c | 46 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index ae4d177083c..8491ab794b4 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -142,6 +142,13 @@ struct Program { int pmt_found; }; +/* Subset of struct Program needed by find_matching_stream(). Keeping this + * small avoids a ~131 KiB stack frame in pmt_cb(). */ +struct ProgramStreams { + unsigned int nb_streams; + struct Stream streams[MAX_STREAMS_PER_PROGRAM]; +}; + struct MpegTSContext { const AVClass *class; /* user data */ @@ -332,6 +339,26 @@ static void clear_program(struct Program *p) p->pmt_found = 0; } +/* Swap two programs without a struct Program-sized stack temporary. */ +static void program_swap(struct Program *a, struct Program *b) +{ + uint8_t *pa = (uint8_t *)a, *pb = (uint8_t *)b; + uint8_t tmp[1024]; + + if (a == b) + return; + + for (size_t rem = sizeof(*a); rem > 0; ) { + size_t chunk = FFMIN(rem, sizeof(tmp)); + memcpy(tmp, pa, chunk); + memcpy(pa, pb, chunk); + memcpy(pb, tmp, chunk); + pa += chunk; + pb += chunk; + rem -= chunk; + } +} + static void clear_programs(MpegTSContext *ts) { av_freep(&ts->prg); @@ -2545,7 +2572,8 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type } static AVStream *find_matching_stream(MpegTSContext *ts, int pid, unsigned int programid, - int stream_identifier, int pmt_stream_idx, struct Program *p) + int stream_identifier, int pmt_stream_idx, + const struct ProgramStreams *p) { AVFormatContext *s = ts->stream; AVStream *found = NULL; @@ -2741,7 +2769,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len { MpegTSContext *ts = filter->u.section_filter.opaque; MpegTSSectionFilter *tssf = &filter->u.section_filter; - struct Program old_program; + struct ProgramStreams old_program; SectionHeader h1, *h = &h1; PESContext *pes; AVStream *st; @@ -2777,15 +2805,17 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len return; prg = get_program(ts, h->id); - if (prg) - old_program = *prg; - else - clear_program(&old_program); - if (ts->skip_unknown_pmt && !prg) return; if (prg && prg->nb_pids && prg->pids[0] != ts->current_pid) return; + if (prg && prg->nb_streams) { + old_program.nb_streams = prg->nb_streams; + memcpy(old_program.streams, prg->streams, + prg->nb_streams * sizeof(*old_program.streams)); + } else + old_program.nb_streams = 0; + if (!ts->skip_clear) clear_avprogram(ts, h->id); clear_program(prg); @@ -3033,7 +3063,7 @@ static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len clear_program(prg); add_pid_to_program(prg, pmt_pid); if (prg_idx > nb_prg) - FFSWAP(struct Program, ts->prg[nb_prg], ts->prg[prg_idx]); + program_swap(&ts->prg[nb_prg], &ts->prg[prg_idx]); if (prg_idx >= nb_prg) nb_prg++; } else -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
