An unquoted bootconfig value can contain both single and double quote
characters, for example:
message = He said "it's fine"
xbc_show_value() surrounds any value containing a double quote with
single quotes. If the value also contains a single quote, both compact
and list output become invalid bootconfig syntax and cannot be parsed
again.
Print values containing both quote types without outer quotes. Such
values can only come from unquoted input, so they do not contain the
delimiters that would require quoting. Keep using single or double
quotes for the other values. Add round-trip checks for compact and list
output, including an array containing the mixed-quote value.
Fixes: 950313ebf79c ("tools: bootconfig: Add bootconfig command")
Signed-off-by: Eduardo Mucelli Rezende Oliveira <redacted>
---
The parser accepts message = He said "it's fine", but the original tool
prints a value surrounded by single quotes. The apostrophe in "it's"
then makes that output invalid.
I checked scalar and array values through compact and list output,
including literal backslashes, quote controls, and a test initrd image.
With the fix, all 25 focused checks pass, independently and in the
combined series, and the parsed values survive those round trips.
The full bootconfig test suite also passes.
tools/bootconfig/main.c | 14 ++++++++------
tools/bootconfig/test-bootconfig.sh | 11 +++++++++++
2 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
index 1bab0e685e2d..7042bb5fe280 100644
--- a/tools/bootconfig/main.c
+++ b/tools/bootconfig/main.c
@@ -52,17 +52,19 @@ static int init_xbc_with_error(char *buf, int len)
static int xbc_show_value(struct xbc_node *node, bool semicolon)
{
- const char *val, *eol;
- char q;
+ const char *val, *eol, *q;
int i = 0;
eol = semicolon ? ";\n" : "\n";
xbc_array_for_each_value(node, val) {
- if (strchr(val, '"'))
- q = '\'';
+ if (!strchr(val, '"'))
+ q = "\"";
+ else if (!strchr(val, '\''))
+ q = "'";
else
- q = '"';
- printf("%c%s%c%s", q, val, q, xbc_node_is_array(node) ? ", " : eol);
+ /* Both types of quotes can only occur in unquoted values. */
+ q = "";
+ printf("%s%s%s%s", q, val, q, xbc_node_is_array(node) ? ", " : eol);
i++;
}
return i;diff --git a/tools/bootconfig/test-bootconfig.sh b/tools/bootconfig/test-bootconfig.sh
index a2e8311d4d19..a0578013e731 100755
--- a/tools/bootconfig/test-bootconfig.sh
+++ b/tools/bootconfig/test-bootconfig.sh
@@ -146,6 +146,17 @@ $BOOTCONF $INITRD > $TEMPCONF
cat $TEMPCONF
xpass grep \'\"string\"\' $TEMPCONF
+echo "Round-trip values containing both types of quotes"
+cat > $TEMPCONF << 'EOF'
+key = "first", He said "it's fine", "last"
+EOF
+for mode in "" "-l"; do
+ $BOOTCONF $mode $TEMPCONF > $OUTFILE
+ xpass $BOOTCONF -a $OUTFILE $INITRD
+ $BOOTCONF -l $INITRD > $OUTFILE
+ xpass cmp $TEMPCONF $OUTFILE
+done
+
echo "Repeat same-key tree"
cat > $TEMPCONF << EOF
foo
--
2.53.0