| From 26fa92f351d4993428268afebf6c60c987477af5 Mon Sep 17 00:00:00 2001 | |
| From: Eduardo Mucelli Rezende Oliveira <edumucelli@gmail.com> | |
| Date: Tue, 10 Nov 2026 20:08:45 +0100 | |
| Subject: tools/bootconfig: Close only opened braces in compact output | |
| From: Eduardo Mucelli Rezende Oliveira <edumucelli@gmail.com> | |
| commit 26fa92f351d4993428268afebf6c60c987477af5 upstream. | |
| A key may have both a value and subkeys. When it has only one subkey, | |
| xbc_show_compact_tree() can print that subkey using dotted notation | |
| without opening a brace. During backtracking, however, it treats the | |
| value node followed by a subkey as evidence that a brace was opened. | |
| For example, a.b=v, a.b.c=w and a.z=q are printed with the brace for a | |
| closed before z. Reading that output back changes a.z into the root key | |
| z, silently changing the configuration. | |
| Check the key children when deciding whether to close a brace. Skip the | |
| value child, and account for a sole subkey that itself has both a value | |
| and subkeys, since that combination is not compacted. This follows the | |
| conditions used to open braces without keeping a depth-indexed array. | |
| Extend the mixed-value/subkey sample to cover the lost sibling. | |
| Link: https://lore.kernel.org/all/6ad67e4cda05238cd173c99e820a85de2c13f5fc.1794337724.git.edumucelli@gmail.com/ | |
| Fixes: e5efaeb8a8f5 ("bootconfig: Support mixing a value and subkeys under a key") | |
| Cc: stable@vger.kernel.org | |
| Signed-off-by: Eduardo Mucelli Rezende Oliveira <edumucelli@gmail.com> | |
| Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> | |
| Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> | |
| --- | |
| tools/bootconfig/main.c | 29 ++++++++++++++++------ | |
| tools/bootconfig/samples/exp-good-mixed-kv1.bconf | 5 +++ | |
| tools/bootconfig/samples/good-mixed-kv1.bconf | 5 +++ | |
| 3 files changed, 32 insertions(+), 7 deletions(-) | |
| --- a/tools/bootconfig/main.c | |
| +++ b/tools/bootconfig/main.c | |
| @@ -39,6 +39,23 @@ static int xbc_show_value(struct xbc_nod | |
| return i; | |
| } | |
| +/* Match the conditions under which the compact formatter opens braces. */ | |
| +static bool xbc_node_needs_braces(struct xbc_node *node) | |
| +{ | |
| + struct xbc_node *child = xbc_node_get_child(node); | |
| + | |
| + if (child && xbc_node_is_value(child)) | |
| + child = xbc_node_get_next(child); | |
| + if (!child) | |
| + return false; | |
| + if (child->next) | |
| + return true; | |
| + | |
| + /* A single subkey with both a value and subkeys is not compacted. */ | |
| + child = xbc_node_get_child(child); | |
| + return child && xbc_node_is_value(child) && child->next; | |
| +} | |
| + | |
| static void xbc_show_compact_tree(void) | |
| { | |
| struct xbc_node *node, *cnode = NULL, *vnode; | |
| @@ -99,14 +116,12 @@ static void xbc_show_compact_tree(void) | |
| node = xbc_node_get_parent(node); | |
| if (!node) | |
| return; | |
| - if (!xbc_node_get_child(node)->next) | |
| + if (!depth || !xbc_node_needs_braces(node)) | |
| continue; | |
| - if (depth) { | |
| - depth--; | |
| - for (i = 0; i < depth; i++) | |
| - printf("\t"); | |
| - printf("}\n"); | |
| - } | |
| + depth--; | |
| + for (i = 0; i < depth; i++) | |
| + printf("\t"); | |
| + printf("}\n"); | |
| } | |
| node = xbc_node_get_next(node); | |
| } | |
| --- a/tools/bootconfig/samples/exp-good-mixed-kv1.bconf | |
| +++ b/tools/bootconfig/samples/exp-good-mixed-kv1.bconf | |
| @@ -1,2 +1,7 @@ | |
| key = "value"; | |
| key.subkey = "another-value"; | |
| +a { | |
| + b = "v"; | |
| + b.c = "w"; | |
| + z = "q"; | |
| +} | |
| --- a/tools/bootconfig/samples/good-mixed-kv1.bconf | |
| +++ b/tools/bootconfig/samples/good-mixed-kv1.bconf | |
| @@ -1,3 +1,8 @@ | |
| # value -> subkey pattern | |
| key = value | |
| key.subkey = another-value | |
| + | |
| +# A valued parent with one subkey must not close its enclosing brace. | |
| +a.b = v | |
| +a.b.c = w | |
| +a.z = q |