Skip to contentSkip to conversation

bootconfig: Fix quote-character issue and return value

PATCH New Masami Hiramatsu 1 participant 5 messages Jun 16, 2020

Subsystems (3)
lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

v2 cover bootconfig: Fix quote-character issue and return value

Hi,

This is the 2nd version of bootconfig bugfixes.
The previous version is here.

https://lkml.kernel.org/r/159197538852.80267.10091816844311950396.stgit@devnote2

This version fixes the patch description and modify(cleanup) code according to Steve's comment.

Thank you,

---

Masami Hiramatsu (4):
proc/bootconfig: Fix to use correct quotes for value tools/bootconfig: Fix to use correct quotes for value tools/bootconfig: Fix to return 0 if succeeded to show the bootconfig tools/bootconfig: Add testcase for show-command and quotes test

 fs/proc/bootconfig.c                |   15 ++++++++++-----
 tools/bootconfig/main.c             |   24 ++++++++++++++----------
 tools/bootconfig/test-bootconfig.sh |   10 ++++++++++
 3 files changed, 34 insertions(+), 15 deletions(-)

--
Masami Hiramatsu (Linaro) <[email protected]>

author maintainer
#1 lore raw

PATCH v2 1/4 proc/bootconfig: Fix to use correct quotes for value

Fix /proc/bootconfig to select double or single quotes corrctly according to the value.

If a bootconfig value includes a double quote character, we must use single-quotes to quote that value.

This modifies if() condition and blocks for avoiding double-quote in value check in 2 places. Anyway, since xbc_array_for_each_value() can handle the array which has a single node correctly.
Thus,

if (vnode && xbc_node_is_array(vnode)) {
	xbc_array_for_each_value(vnode)	/* vnode->next != NULL */
		...
} else {

snprintf(val); /* val is an empty string if !vnode */
}

is equivalent to

if (vnode) {
	xbc_array_for_each_value(vnode)	/* vnode->next can be NULL */
		...
} else {

snprintf(""); /* value is always empty */
}

Fixes c1a3c36017d4 ("proc: bootconfig: Add /proc/bootconfig to show boot config list")

---
Changes in v2:
- Sort local vars definition as upside-down xmas tree format.
- Fix patch description (typo and add comments)

---
 fs/proc/bootconfig.c |   15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
fs/proc/bootconfig.c +10 -5
index 9955d75c0585..ad31ec4ad627 100644
@@ -26,8 +26,9 @@ static int boot_config_proc_show(struct seq_file *m, void *v)
 static int __init copy_xbc_key_value_list(char *dst, size_t size)
 {
 	struct xbc_node *leaf, *vnode;
-	const char *val;
 	char *key, *end = dst + size;
+	const char *val;
+	char q;
 	int ret = 0;
 
 	key = kzalloc(XBC_KEYLEN_MAX, GFP_KERNEL);
@@ -41,16 +42,20 @@ static int __init copy_xbc_key_value_list(char *dst, size_t size)
 			break;
 		dst += ret;
 		vnode = xbc_node_get_child(leaf);
-		if (vnode && xbc_node_is_array(vnode)) {
+		if (vnode) {
 			xbc_array_for_each_value(vnode, val) {
-				ret = snprintf(dst, rest(dst, end), "\"%s\"%s",
-					val, vnode->next ? ", " : "\n");
+				if (strchr(val, '"'))
+					q = '\'';
+				else
+					q = '"';
+				ret = snprintf(dst, rest(dst, end), "%c%s%c%s",
+					q, val, q, vnode->next ? ", " : "\n");
 				if (ret < 0)
 					goto out;
 				dst += ret;
 			}
 		} else {
-			ret = snprintf(dst, rest(dst, end), "\"%s\"\n", val);
+			ret = snprintf(dst, rest(dst, end), "\"\"\n");
 			if (ret < 0)
 				break;
 			dst += ret;
author maintainer
#2 lore raw

PATCH v2 2/4 tools/bootconfig: Fix to use correct quotes for value

In reply to Masami Hiramatsu

Fix bootconfig tool to select double or single quotes correctly according to the value.

If a bootconfig value includes a double quote character, we must use single-quotes to quote that value.

Fixes 950313ebf79c ("tools: bootconfig: Add bootconfig command")

---
Changes in v2:
- Fix patch description.

---
 tools/bootconfig/main.c |   14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
tools/bootconfig/main.c +8 -6
index 0efaf45f7367..21896a6675fd 100644
@@ -14,13 +14,18 @@
 #include <linux/kernel.h>
 #include <linux/bootconfig.h>
 
-static int xbc_show_array(struct xbc_node *node)
+static int xbc_show_value(struct xbc_node *node)
 {
 	const char *val;
+	char q;
 	int i = 0;
 
 	xbc_array_for_each_value(node, val) {
-		printf("\"%s\"%s", val, node->next ? ", " : ";\n");
+		if (strchr(val, '"'))
+			q = '\'';
+		else
+			q = '"';
+		printf("%c%s%c%s", q, val, q, node->next ? ", " : ";\n");
 		i++;
 	}
 	return i;
@@ -48,10 +53,7 @@ static void xbc_show_compact_tree(void)
 			continue;
 		} else if (cnode && xbc_node_is_value(cnode)) {
 			printf("%s = ", xbc_node_get_data(node));
-			if (cnode->next)
-				xbc_show_array(cnode);
-			else
-				printf("\"%s\";\n", xbc_node_get_data(cnode));
+			xbc_show_value(cnode);
 		} else {
 			printf("%s;\n", xbc_node_get_data(node));
 		}
author maintainer
#3 lore raw

PATCH v2 3/4 tools/bootconfig: Fix to return 0 if succeeded to show the bootconfig

In reply to Masami Hiramatsu

Fix bootconfig to return 0 if succeeded to show the bootconfig in initrd. Without this fix, "bootconfig INITRD" command returns !0 even if the command succeeded to show the bootconfig.

Fixes 950313ebf79c ("tools: bootconfig: Add bootconfig command")

---
Changes in v2:
- Use goto for better reading.

---
 tools/bootconfig/main.c |   10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
tools/bootconfig/main.c +6 -4
index 21896a6675fd..e0878f5f74b1 100644
@@ -207,11 +207,13 @@ int show_xbc(const char *path)
 	}
 
 	ret = load_xbc_from_initrd(fd, &buf);
-	if (ret < 0)
+	if (ret < 0) {
 		pr_err("Failed to load a boot config from initrd: %d\n", ret);
-	else
-		xbc_show_compact_tree();
-
+		goto out;
+	}
+	xbc_show_compact_tree();
+	ret = 0;
+out:
 	close(fd);
 	free(buf);
 
author maintainer
#4 lore raw

PATCH v2 4/4 tools/bootconfig: Add testcase for show-command and quotes test

In reply to Masami Hiramatsu

Add testcases for the return value of the command to show bootconfig in initrd, and double/single quotes selecting.

---
Changes in v2:
- Update patch description.

---
 tools/bootconfig/test-bootconfig.sh |   10 ++++++++++
 1 file changed, 10 insertions(+)
tools/bootconfig/test-bootconfig.sh +10 -0
index eff16b77d5eb..3c2ab9e75730 100755
@@ -55,6 +55,9 @@ echo "Apply command test"
 xpass $BOOTCONF -a $TEMPCONF $INITRD
 new_size=$(stat -c %s $INITRD)
 
+echo "Show command test"
+xpass $BOOTCONF $INITRD
+
 echo "File size check"
 xpass test $new_size -eq $(expr $bconf_size + $initrd_size + 9 + 12)
 
@@ -114,6 +117,13 @@ xpass grep -q "bar" $OUTFILE
 xpass grep -q "baz" $OUTFILE
 xpass grep -q "qux" $OUTFILE
 
+echo "Double/single quotes test"
+echo "key = '\"string\"';" > $TEMPCONF
+$BOOTCONF -a $TEMPCONF $INITRD
+$BOOTCONF $INITRD > $TEMPCONF
+cat $TEMPCONF
+xpass grep \'\"string\"\' $TEMPCONF
+
 echo "=== expected failure cases ==="
 for i in samples/bad-* ; do
   xfail $BOOTCONF -a $i $INITRD