Skip to contentSkip to conversation

dtc: Implement checks for the format of node and property names

PATCH David Gibson 1 participant 1 message Feb 27, 2008

lore
Share & tools
lore
#0 lore raw

This patch adds checks to the checking framework to verify that node and property names contain only legal characters, and in the case of node names there is at most one '@'.

At present when coming from dts input, this is mostly already ensured by the grammer, however putting the check later means its easier to generate helpful error messages rather than just "syntax error". For dtb input, these checks replace the older similar check built into flattree.c.

Testcases for the checks are also implemented.

---
 checks.c           |   37 +++++++++++++++++++++++++++++++++++++
 flattree.c         |   28 +++++-----------------------
 tests/dumptrees.c  |    1 +
 tests/run_tests.sh |    3 +++
 tests/testdata.h   |    3 +++
 tests/trees.S      |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 97 insertions(+), 23 deletions(-)
checks.c +37 -0
Index: dtc/checks.c
===================================================================
@@ -242,6 +242,42 @@
 }
 NODE_CHECK(duplicate_property_names, NULL, ERROR);
 
+#define LOWERCASE	"abcdefghijklmnopqrstuvwxyz"
+#define UPPERCASE	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+#define DIGITS		"0123456789"
+#define PROPNODECHARS	LOWERCASE UPPERCASE DIGITS ",._+*#?-"
+
+static void check_node_name_chars(struct check *c, struct node *dt,
+				  struct node *node)
+{
+	int n = strspn(node->name, c->data);
+
+	if (n < strlen(node->name))
+		FAIL(c, "Bad character '%c' in node %s",
+		     node->name[n], node->fullpath);
+}
+NODE_CHECK(node_name_chars, PROPNODECHARS "@", ERROR);
+
+static void check_node_name_format(struct check *c, struct node *dt,
+				   struct node *node)
+{
+	if (strchr(get_unitname(node), '@'))
+		FAIL(c, "Node %s has multiple '@' characters in name",
+		     node->fullpath);
+}
+NODE_CHECK(node_name_format, NULL, ERROR, &node_name_chars);
+
+static void check_property_name_chars(struct check *c, struct node *dt,
+				      struct node *node, struct property *prop)
+{
+	int n = strspn(prop->name, c->data);
+
+	if (n < strlen(prop->name))
+		FAIL(c, "Bad character '%c' in property name \"%s\", node %s",
+		     prop->name[n], prop->name, node->fullpath);
+}
+PROP_CHECK(property_name_chars, PROPNODECHARS, ERROR);
+
 static void check_explicit_phandles(struct check *c, struct node *root,
 					  struct node *node)
 {
@@ -498,6 +534,7 @@
 
 static struct check *check_table[] = {
 	&duplicate_node_names, &duplicate_property_names,
+	&node_name_chars, &node_name_format, &property_name_chars,
 	&name_is_string, &name_properties,
 	&explicit_phandles,
 	&phandle_references, &path_references,
tests/trees.S +48 -0

51 lines not on this page · raw message

flattree.c +5 -23
Index: dtc/flattree.c
===================================================================
@@ -729,29 +729,14 @@
 	return strdup(lslash+1);
 }
 
-static const char PROPCHAR[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,._+*#?-";
-static const char UNITCHAR[] = "0123456789abcdef,";
-
-static int check_node_name(const char *name)
+static int find_basenamelen(const char *name)
 {
-	const char *atpos;
-	int basenamelen;
-
-	atpos = strrchr(name, '@');
+	const char *atpos = strchr(name, '@');
 
 	if (atpos)
-		basenamelen = atpos - name;
+		return atpos - name;
 	else
-		basenamelen = strlen(name);
-
-	if (strspn(name, PROPCHAR) < basenamelen)
-		return -1;
-
-	if (atpos
-	    && ((basenamelen + 1 + strspn(atpos+1, UNITCHAR)) < strlen(name)))
-		return -1;
-
-	return basenamelen;
+		return strlen(name);
 }
 
 static struct node *unflatten_tree(struct inbuf *dtbuf,
@@ -775,10 +760,7 @@
 		node->fullpath = join_path(parent_path, node->name);
 	}
 
-	node->basenamelen = check_node_name(node->name);
-	if (node->basenamelen < 0) {
-		fprintf(stderr, "Warning \"%s\" has incorrect format\n", node->name);
-	}
+	node->basenamelen = find_basenamelen(node->name);
 
 	do {
 		struct property *prop;
tests/dumptrees.c +1 -0
Index: dtc/tests/dumptrees.c
===================================================================
@@ -37,6 +37,7 @@
 } trees[] = {
 #define TREE(name)	{ &_##name, #name ".dtb" }
 	TREE(test_tree1),
+	TREE(bad_node_char), TREE(bad_node_format), TREE(bad_prop_char),
 };
 
 #define NUM_TREES	(sizeof(trees) / sizeof(trees[0]))
tests/run_tests.sh +3 -0
Index: dtc/tests/run_tests.sh
===================================================================
@@ -189,6 +189,9 @@
     run_test dtc-checkfails.sh ranges_format -- -I dts -O dtb bad-empty-ranges.dts
     run_test dtc-checkfails.sh avoid_default_addr_size -- -I dts -O dtb default-addr-size.dts
     run_test dtc-checkfails.sh obsolete_chosen_interrupt_controller -- -I dts -O dtb obsolete-chosen-interrupt-controller.dts
+    run_test dtc-checkfails.sh node_name_chars -- -I dtb -O dtb bad_node_char.dtb
+    run_test dtc-checkfails.sh node_name_format -- -I dtb -O dtb bad_node_format.dtb
+    run_test dtc-checkfails.sh prop_name_chars -- -I dtb -O dtb bad_prop_char.dtb
 }
 
 while getopts "vt:m" ARG ; do
tests/testdata.h +3 -0
Index: dtc/tests/testdata.h
===================================================================
@@ -33,4 +33,7 @@
 #ifndef __ASSEMBLY__
 extern struct fdt_header _test_tree1;
 extern struct fdt_header _truncated_property;
+extern struct fdt_header _bad_node_char;
+extern struct fdt_header _bad_node_format;
+extern struct fdt_header _bad_prop_char;
 #endif /* ! __ASSEMBLY */
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson