From a56fbb903539fefb1265b1faaf0954b931978c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Li=C5=A1tiak?= Date: Mon, 21 Sep 2026 15:04:51 +0200 Subject: [PATCH] list: preserve anonymous flag on type change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When uci_set() changes a section type to a value with a different length, the section is reallocated with uci_alloc_section(). For an anonymous section, the generated section name is passed to the allocator. Since uci_alloc_section() derives the anonymous flag from whether the name is NULL, the newly allocated section becomes named. This causes an anonymous section to be exported with its internal cfg... name and to remain named after the configuration is reloaded. Preserve the anonymous flag of the old section across the reallocation. Add a regression test covering a type change of an anonymous section while preserving its existing options. Signed-off-by: Peter Lištiak --- list.c | 1 + tests/shunit2/tests.d/030_set | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/list.c b/list.c index 304c9e1..d6bd0bc 100644 --- a/list.c +++ b/list.c @@ -730,6 +730,7 @@ int uci_set(struct uci_context *ctx, struct uci_ptr *ptr) } else { struct uci_section *old = ptr->s; ptr->s = uci_alloc_section(ptr->p, ptr->value, old->e.name, &old->e.list); + ptr->s->anonymous = old->anonymous; uci_section_transfer_options(ptr->s, old); if (ptr->section == old->e.name) ptr->section = ptr->s->e.name; diff --git a/tests/shunit2/tests.d/030_set b/tests/shunit2/tests.d/030_set index db3c259..4aadda3 100644 --- a/tests/shunit2/tests.d/030_set +++ b/tests/shunit2/tests.d/030_set @@ -44,3 +44,19 @@ test_set_existing_option_multiline() World\"" assertSameFile ${REF_DIR}/set_existing_option_multiline.result ${CHANGES_DIR}/set } + +test_set_anonymous_section_type_preserves_anonymous() +{ + cat > ${CONFIG_DIR}/set << 'EOF_CONFIG' +config short + option value 'kept' +EOF_CONFIG + + ${UCI} set 'set.@short[0]=longer' + ${UCI} commit set + + output=$(${UCI} export set) + echo "$output" | grep -q '^config longer$' + assertTrue "changing an anonymous section type makes it named" $? + assertEquals "kept" "$(${UCI} get 'set.@longer[0].value')" +}