[PATCH uci] list: preserve anonymous flag on type change
Peter Lištiak
peter at listiak.eu
Wed Oct 21 08:00:50 PDT 2026
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 <peter at listiak.eu>
---
I ran into this while working on preserving comments when modifying UCI
configuration files. The bug is unrelated to that work, so I split it out
as a standalone fix.
This patch is also available as GitHub PR #27:
https://github.com/openwrt/uci/pull/27
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. at 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. at longer[0].value')"
+}
--
2.43.0
More information about the openwrt-devel
mailing list