summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2023-02-27 23:18:52 +1100
committerDavid Gibson <david@gibson.dropbear.id.au>2023-03-01 17:55:35 +1100
commitfd68bb8c56587114fa6eeed39c36c4b347a6a5fc (patch)
tree6f70106ddda74fab125f50471f489690e192180a
parent4718189c4ca83ddaa38a4990bddd1d4196b11933 (diff)
downloaddevice-tree-compiler-fd68bb8c56587114fa6eeed39c36c4b347a6a5fc.tar.gz
Make name_node() xstrdup its name argument
The name field of 'struct node' was really always supposed to be a malloc()ed string, that is owned by the structure. To avoid an extra strdup() for strings coming up from the lexer, name_node() expects to take uch an already malloc()ed string, which means it's not correct to pass it a static string literal. That's a pretty non-obvious constraint, so a bunch of incorrect uses have crept in. Really, avoiding the extra dup from the lexer isn't a big enough benefit for this demonstrably dangerous interface. So change it to do the xstrdup() itself, removing the burden from callers. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
-rw-r--r--dtc-parser.y2
-rw-r--r--dtc.h8
-rw-r--r--livetree.c17
3 files changed, 15 insertions, 12 deletions
diff --git a/dtc-parser.y b/dtc-parser.y
index 9b724e5..4d5eece 100644
--- a/dtc-parser.y
+++ b/dtc-parser.y
@@ -573,10 +573,12 @@ subnode:
DT_PROPNODENAME nodedef
{
$$ = name_node($2, $1);
+ free($1);
}
| DT_DEL_NODE DT_PROPNODENAME ';'
{
$$ = name_node(build_node_delete(&@$), $2);
+ free($2);
}
| DT_OMIT_NO_REF subnode
{
diff --git a/dtc.h b/dtc.h
index 0fca726..4c4aaca 100644
--- a/dtc.h
+++ b/dtc.h
@@ -269,7 +269,7 @@ struct property *reverse_properties(struct property *first);
struct node *build_node(struct property *proplist, struct node *children,
struct srcpos *srcpos);
struct node *build_node_delete(struct srcpos *srcpos);
-struct node *name_node(struct node *node, char *name);
+struct node *name_node(struct node *node, const char *name);
struct node *omit_node_if_unused(struct node *node);
struct node *reference_node(struct node *node);
struct node *chain_node(struct node *first, struct node *list);
@@ -336,9 +336,9 @@ struct dt_info *build_dt_info(unsigned int dtsflags,
struct reserve_info *reservelist,
struct node *tree, uint32_t boot_cpuid_phys);
void sort_tree(struct dt_info *dti);
-void generate_label_tree(struct dt_info *dti, char *name, bool allocph);
-void generate_fixups_tree(struct dt_info *dti, char *name);
-void generate_local_fixups_tree(struct dt_info *dti, char *name);
+void generate_label_tree(struct dt_info *dti, const char *name, bool allocph);
+void generate_fixups_tree(struct dt_info *dti, const char *name);
+void generate_local_fixups_tree(struct dt_info *dti, const char *name);
/* Checks */
diff --git a/livetree.c b/livetree.c
index f76ad7f..803093f 100644
--- a/livetree.c
+++ b/livetree.c
@@ -116,11 +116,11 @@ struct node *build_node_delete(struct srcpos *srcpos)
return new;
}
-struct node *name_node(struct node *node, char *name)
+struct node *name_node(struct node *node, const char *name)
{
assert(node->name == NULL);
- node->name = name;
+ node->name = xstrdup(name);
return node;
}
@@ -250,6 +250,7 @@ struct node * add_orphan_node(struct node *dt, struct node *new_node, char *ref)
name_node(new_node, "__overlay__");
node = build_node(p, new_node, NULL);
name_node(node, name);
+ free(name);
add_child(dt, node);
return dt;
@@ -808,18 +809,18 @@ void sort_tree(struct dt_info *dti)
}
/* utility helper to avoid code duplication */
-static struct node *build_and_name_child_node(struct node *parent, char *name)
+static struct node *build_and_name_child_node(struct node *parent, const char *name)
{
struct node *node;
node = build_node(NULL, NULL, NULL);
- name_node(node, xstrdup(name));
+ name_node(node, name);
add_child(parent, node);
return node;
}
-static struct node *build_root_node(struct node *dt, char *name)
+static struct node *build_root_node(struct node *dt, const char *name)
{
struct node *an;
@@ -1040,7 +1041,7 @@ static void generate_local_fixups_tree_internal(struct dt_info *dti,
generate_local_fixups_tree_internal(dti, lfn, c);
}
-void generate_label_tree(struct dt_info *dti, char *name, bool allocph)
+void generate_label_tree(struct dt_info *dti, const char *name, bool allocph)
{
if (!any_label_tree(dti, dti->dt))
return;
@@ -1048,7 +1049,7 @@ void generate_label_tree(struct dt_info *dti, char *name, bool allocph)
dti->dt, allocph);
}
-void generate_fixups_tree(struct dt_info *dti, char *name)
+void generate_fixups_tree(struct dt_info *dti, const char *name)
{
if (!any_fixup_tree(dti, dti->dt))
return;
@@ -1056,7 +1057,7 @@ void generate_fixups_tree(struct dt_info *dti, char *name)
dti->dt);
}
-void generate_local_fixups_tree(struct dt_info *dti, char *name)
+void generate_local_fixups_tree(struct dt_info *dti, const char *name)
{
if (!any_local_fixup_tree(dti, dti->dt))
return;