Skip to content

Commit fd68bb8

Browse files
committed
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 <[email protected]>
1 parent 4718189 commit fd68bb8

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

dtc-parser.y

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,12 @@ subnode:
573573
DT_PROPNODENAME nodedef
574574
{
575575
$$ = name_node($2, $1);
576+
free($1);
576577
}
577578
| DT_DEL_NODE DT_PROPNODENAME ';'
578579
{
579580
$$ = name_node(build_node_delete(&@$), $2);
581+
free($2);
580582
}
581583
| DT_OMIT_NO_REF subnode
582584
{

dtc.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ struct property *reverse_properties(struct property *first);
269269
struct node *build_node(struct property *proplist, struct node *children,
270270
struct srcpos *srcpos);
271271
struct node *build_node_delete(struct srcpos *srcpos);
272-
struct node *name_node(struct node *node, char *name);
272+
struct node *name_node(struct node *node, const char *name);
273273
struct node *omit_node_if_unused(struct node *node);
274274
struct node *reference_node(struct node *node);
275275
struct node *chain_node(struct node *first, struct node *list);
@@ -336,9 +336,9 @@ struct dt_info *build_dt_info(unsigned int dtsflags,
336336
struct reserve_info *reservelist,
337337
struct node *tree, uint32_t boot_cpuid_phys);
338338
void sort_tree(struct dt_info *dti);
339-
void generate_label_tree(struct dt_info *dti, char *name, bool allocph);
340-
void generate_fixups_tree(struct dt_info *dti, char *name);
341-
void generate_local_fixups_tree(struct dt_info *dti, char *name);
339+
void generate_label_tree(struct dt_info *dti, const char *name, bool allocph);
340+
void generate_fixups_tree(struct dt_info *dti, const char *name);
341+
void generate_local_fixups_tree(struct dt_info *dti, const char *name);
342342

343343
/* Checks */
344344

livetree.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ struct node *build_node_delete(struct srcpos *srcpos)
116116
return new;
117117
}
118118

119-
struct node *name_node(struct node *node, char *name)
119+
struct node *name_node(struct node *node, const char *name)
120120
{
121121
assert(node->name == NULL);
122122

123-
node->name = name;
123+
node->name = xstrdup(name);
124124

125125
return node;
126126
}
@@ -250,6 +250,7 @@ struct node * add_orphan_node(struct node *dt, struct node *new_node, char *ref)
250250
name_node(new_node, "__overlay__");
251251
node = build_node(p, new_node, NULL);
252252
name_node(node, name);
253+
free(name);
253254

254255
add_child(dt, node);
255256
return dt;
@@ -808,18 +809,18 @@ void sort_tree(struct dt_info *dti)
808809
}
809810

810811
/* utility helper to avoid code duplication */
811-
static struct node *build_and_name_child_node(struct node *parent, char *name)
812+
static struct node *build_and_name_child_node(struct node *parent, const char *name)
812813
{
813814
struct node *node;
814815

815816
node = build_node(NULL, NULL, NULL);
816-
name_node(node, xstrdup(name));
817+
name_node(node, name);
817818
add_child(parent, node);
818819

819820
return node;
820821
}
821822

822-
static struct node *build_root_node(struct node *dt, char *name)
823+
static struct node *build_root_node(struct node *dt, const char *name)
823824
{
824825
struct node *an;
825826

@@ -1040,23 +1041,23 @@ static void generate_local_fixups_tree_internal(struct dt_info *dti,
10401041
generate_local_fixups_tree_internal(dti, lfn, c);
10411042
}
10421043

1043-
void generate_label_tree(struct dt_info *dti, char *name, bool allocph)
1044+
void generate_label_tree(struct dt_info *dti, const char *name, bool allocph)
10441045
{
10451046
if (!any_label_tree(dti, dti->dt))
10461047
return;
10471048
generate_label_tree_internal(dti, build_root_node(dti->dt, name),
10481049
dti->dt, allocph);
10491050
}
10501051

1051-
void generate_fixups_tree(struct dt_info *dti, char *name)
1052+
void generate_fixups_tree(struct dt_info *dti, const char *name)
10521053
{
10531054
if (!any_fixup_tree(dti, dti->dt))
10541055
return;
10551056
generate_fixups_tree_internal(dti, build_root_node(dti->dt, name),
10561057
dti->dt);
10571058
}
10581059

1059-
void generate_local_fixups_tree(struct dt_info *dti, char *name)
1060+
void generate_local_fixups_tree(struct dt_info *dti, const char *name)
10601061
{
10611062
if (!any_local_fixup_tree(dti, dti->dt))
10621063
return;

0 commit comments

Comments
 (0)