From c010b1695dcf96ad5ff9b4c1228d0d8d789848d7 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Fri, 25 Sep 2026 15:45:20 -0700 Subject: [PATCH 1/4] upipe_dup: hand the original to the last sub as intended The loop declared a second variable named output for the current sub, so the test that the dup has no direct output read the sub pointer instead and never held: every sub got a copy and the original was freed after the loop, one uref_dup and one uref_free per input more than written. The dup test now checks which sink receives the original. --- lib/upipe-modules/upipe_dup.c | 7 ++++--- tests/upipe_dup_test.c | 11 +++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/upipe-modules/upipe_dup.c b/lib/upipe-modules/upipe_dup.c index 1618d31aa..c09510cea 100644 --- a/lib/upipe-modules/upipe_dup.c +++ b/lib/upipe-modules/upipe_dup.c @@ -216,9 +216,10 @@ static void upipe_dup_input(struct upipe *upipe, struct uref *uref, ulist_delete_foreach (&upipe_dup->outputs, uchain, uchain_tmp) { struct upipe_dup_output *upipe_dup_output = upipe_dup_output_from_uchain(uchain); - struct upipe *output = upipe_dup_output_to_upipe(upipe_dup_output); + struct upipe *sub = upipe_dup_output_to_upipe(upipe_dup_output); + /* the last sub takes the original when no direct output wants it */ if (ulist_is_last(&upipe_dup->outputs, uchain) && !output) { - upipe_dup_output_output(output, uref, upump_p); + upipe_dup_output_output(sub, uref, upump_p); uref = NULL; } else { struct uref *new_uref = uref_dup(uref); @@ -227,7 +228,7 @@ static void upipe_dup_input(struct upipe *upipe, struct uref *uref, upipe_throw_fatal(upipe, UBASE_ERR_ALLOC); return; } - upipe_dup_output_output(output, new_uref, upump_p); + upipe_dup_output_output(sub, new_uref, upump_p); } } diff --git a/tests/upipe_dup_test.c b/tests/upipe_dup_test.c index 0cf7a5390..d3ea7520f 100644 --- a/tests/upipe_dup_test.c +++ b/tests/upipe_dup_test.c @@ -35,6 +35,9 @@ #define UPROBE_LOG_LEVEL UPROBE_LOG_DEBUG static int counter = 0; +/* the uref given to the dup, and how many sinks received it rather than a copy */ +static struct uref *original = NULL; +static int originals = 0; static int flow_foo_counter = 0; static int flow_bar_counter = 0; @@ -71,6 +74,8 @@ static void test_input(struct upipe *upipe, struct uref *uref, { assert(uref != NULL); counter++; + if (uref == original) + originals++; uref_free(uref); } @@ -154,8 +159,11 @@ int main(int argc, char *argv[]) uref = uref_alloc(uref_mgr); assert(uref != NULL); + original = uref; upipe_input(upipe_dup, uref, NULL); assert(counter == 1); + /* the only sub takes the original: nothing was copied */ + assert(originals == 1); counter = 0; struct upipe *upipe_dup_output1 = upipe_void_alloc_sub(upipe_dup, @@ -172,8 +180,11 @@ int main(int argc, char *argv[]) uref = uref_alloc(uref_mgr); assert(uref != NULL); + original = uref; upipe_input(upipe_dup, uref, NULL); assert(counter == 2); + /* one copy for the first sub, the original to the last */ + assert(originals == 2); assert(flow_foo_counter == 1); assert(flow_bar_counter == 2); From 459e9efca0f2182cd34f0bb1bc430ff3eff13ddc Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Thu, 24 Sep 2026 13:44:35 -0400 Subject: [PATCH 2/4] upipe_ts_split: hand the original uref to the last sub ulist is circular, so the last sub's uchain->next is the list head and never NULL: every TS packet on every PID was duplicated once per sub and the original freed afterwards. A PID with a single sub now costs no allocation in the split. --- lib/upipe-ts/upipe_ts_split.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/upipe-ts/upipe_ts_split.c b/lib/upipe-ts/upipe_ts_split.c index 9c6266bb0..b23760e98 100644 --- a/lib/upipe-ts/upipe_ts_split.c +++ b/lib/upipe-ts/upipe_ts_split.c @@ -336,7 +336,8 @@ static void upipe_ts_split_input(struct upipe *upipe, struct uref *uref, ulist_delete_foreach(&upipe_ts_split->pids[pid].subs, uchain, uchain_tmp) { struct upipe_ts_split_sub *output = upipe_ts_split_sub_from_uchain_pid(uchain); - if (likely(uchain->next == NULL)) { + /* the list is circular: the last sub is followed by the head */ + if (likely(uchain->next == &upipe_ts_split->pids[pid].subs)) { upipe_ts_split_sub_output(upipe_ts_split_sub_to_upipe(output), uref, upump_p); uref = NULL; From 9d1cde3a997796f1b7bb4bb6348fde391a480f7d Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Thu, 24 Sep 2026 13:45:11 -0400 Subject: [PATCH 3/4] upipe_ts_psi_split: hand the original uref to the last sub Same circular-list test as in upipe_ts_split: every section was duplicated once per sub and the original freed. --- lib/upipe-ts/upipe_ts_psi_split.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/upipe-ts/upipe_ts_psi_split.c b/lib/upipe-ts/upipe_ts_psi_split.c index ab7619598..a3f28950c 100644 --- a/lib/upipe-ts/upipe_ts_psi_split.c +++ b/lib/upipe-ts/upipe_ts_psi_split.c @@ -218,7 +218,8 @@ static void upipe_ts_psi_split_input(struct upipe *upipe, struct uref *uref, if (ubase_check(uref_ts_flow_get_psi_filter(output->flow_def, &filter, &mask, &size)) && ubase_check(uref_block_match(uref, filter, mask, size))) { - if (likely(uchain->next == NULL)) { + /* the list is circular: the last sub is followed by the head */ + if (likely(uchain->next == &upipe_ts_psi_split->subs)) { upipe_ts_psi_split_sub_output( upipe_ts_psi_split_sub_to_upipe(output), uref, upump_p); From e9e7533041bb37c7ce6fb589c8f97367cf861f6b Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Mon, 26 Oct 2026 18:37:49 +0000 Subject: [PATCH 4/4] upipe_ts_psi_split: stop once the last sub has the original The output of the last sub can add a sub to the same list. The loop then went on to it with a NULL uref and crashed in uref_block_match(). --- lib/upipe-ts/upipe_ts_psi_split.c | 3 ++- tests/upipe_ts_psi_split_test.c | 39 +++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/upipe-ts/upipe_ts_psi_split.c b/lib/upipe-ts/upipe_ts_psi_split.c index a3f28950c..6bcf5e411 100644 --- a/lib/upipe-ts/upipe_ts_psi_split.c +++ b/lib/upipe-ts/upipe_ts_psi_split.c @@ -223,7 +223,8 @@ static void upipe_ts_psi_split_input(struct upipe *upipe, struct uref *uref, upipe_ts_psi_split_sub_output( upipe_ts_psi_split_sub_to_upipe(output), uref, upump_p); - uref = NULL; + /* the output may have added a sub after this one */ + return; } else { struct uref *new_uref = uref_dup(uref); if (likely(new_uref != NULL)) diff --git a/tests/upipe_ts_psi_split_test.c b/tests/upipe_ts_psi_split_test.c index 44fef95f5..bfb600279 100644 --- a/tests/upipe_ts_psi_split_test.c +++ b/tests/upipe_ts_psi_split_test.c @@ -71,6 +71,33 @@ struct test { struct upipe upipe; }; +static struct uref_mgr *uref_mgr; +static struct uprobe *uprobe_stdio; +static struct upipe *upipe_ts_psi_split; +static struct upipe *upipe_ts_psi_split_output70 = NULL; + +/** adds a sub from inside the output of the last one */ +static void test_add_sub70(void) +{ + struct uref *flow_def = uref_block_flow_alloc_def(uref_mgr, "mpegtspsi."); + assert(flow_def != NULL); + uint8_t filter[PSI_HEADER_SIZE_SYNTAX1]; + memset(filter, 0, PSI_HEADER_SIZE_SYNTAX1); + filter[1] = 0x80; + uint8_t mask[PSI_HEADER_SIZE_SYNTAX1]; + memset(mask, 0, PSI_HEADER_SIZE_SYNTAX1); + mask[1] = 0x80; + psi_set_tableid(mask, 0xff); + psi_set_tableid(filter, 70); + ubase_assert(uref_ts_flow_set_psi_filter(flow_def, filter, mask, + PSI_HEADER_SIZE_SYNTAX1)); + upipe_ts_psi_split_output70 = upipe_flow_alloc_sub(upipe_ts_psi_split, + uprobe_pfx_alloc(uprobe_use(uprobe_stdio), UPROBE_LOG_LEVEL, + "ts psi split output 70"), flow_def); + assert(upipe_ts_psi_split_output70 != NULL); + uref_free(flow_def); +} + /** helper phony pipe */ static struct upipe *test_alloc(struct upipe_mgr *mgr, struct uprobe *uprobe, uint32_t signature, va_list args) @@ -107,6 +134,8 @@ static void test_input(struct upipe *upipe, struct uref *uref, } uref_block_unmap(uref, 0); uref_free(uref); + if (test->table_id == 69 && upipe_ts_psi_split_output70 == NULL) + test_add_sub70(); } /** helper phony pipe */ @@ -145,8 +174,7 @@ int main(int argc, char *argv[]) struct udict_mgr *udict_mgr = udict_inline_mgr_alloc(UDICT_POOL_DEPTH, umem_mgr, -1, -1); assert(udict_mgr != NULL); - struct uref_mgr *uref_mgr = uref_std_mgr_alloc(UREF_POOL_DEPTH, udict_mgr, - 0); + uref_mgr = uref_std_mgr_alloc(UREF_POOL_DEPTH, udict_mgr, 0); assert(uref_mgr != NULL); struct ubuf_mgr *ubuf_mgr = ubuf_block_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, @@ -154,8 +182,7 @@ int main(int argc, char *argv[]) assert(ubuf_mgr != NULL); struct uprobe uprobe; uprobe_init(&uprobe, catch, NULL); - struct uprobe *uprobe_stdio = uprobe_stdio_alloc(&uprobe, stdout, - UPROBE_LOG_LEVEL); + uprobe_stdio = uprobe_stdio_alloc(&uprobe, stdout, UPROBE_LOG_LEVEL); assert(uprobe_stdio != NULL); struct uref *uref; @@ -164,7 +191,7 @@ int main(int argc, char *argv[]) struct upipe_mgr *upipe_ts_psi_split_mgr = upipe_ts_psi_split_mgr_alloc(); assert(upipe_ts_psi_split_mgr != NULL); - struct upipe *upipe_ts_psi_split = upipe_void_alloc(upipe_ts_psi_split_mgr, + upipe_ts_psi_split = upipe_void_alloc(upipe_ts_psi_split_mgr, uprobe_pfx_alloc(uprobe_use(uprobe_stdio), UPROBE_LOG_LEVEL, "ts psi split")); assert(upipe_ts_psi_split != NULL); @@ -248,6 +275,8 @@ int main(int argc, char *argv[]) upipe_release(upipe_ts_psi_split_output68); upipe_release(upipe_ts_psi_split_output69); + assert(upipe_ts_psi_split_output70 != NULL); + upipe_release(upipe_ts_psi_split_output70); upipe_release(upipe_ts_psi_split); upipe_mgr_release(upipe_ts_psi_split_mgr); // nop