Browse Source

Make build_property() xstrdup its name argument

The name field of 'struct property' 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, build_property() and
build_property_delete() expect to take such 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>
main
David Gibson 2 years ago
parent
commit
0b842c3c81
  1. 3
      dtc-parser.y
  2. 4
      dtc.h
  3. 2
      fstree.c
  4. 8
      livetree.c

3
dtc-parser.y

@ -284,14 +284,17 @@ propdef:
DT_PROPNODENAME '=' propdata ';' DT_PROPNODENAME '=' propdata ';'
{ {
$$ = build_property($1, $3, &@$); $$ = build_property($1, $3, &@$);
free($1);
} }
| DT_PROPNODENAME ';' | DT_PROPNODENAME ';'
{ {
$$ = build_property($1, empty_data, &@$); $$ = build_property($1, empty_data, &@$);
free($1);
} }
| DT_DEL_PROP DT_PROPNODENAME ';' | DT_DEL_PROP DT_PROPNODENAME ';'
{ {
$$ = build_property_delete($2); $$ = build_property_delete($2);
free($2);
} }
| DT_LABEL propdef | DT_LABEL propdef
{ {

4
dtc.h

@ -260,9 +260,9 @@ struct node {
void add_label(struct label **labels, char *label); void add_label(struct label **labels, char *label);
void delete_labels(struct label **labels); void delete_labels(struct label **labels);


struct property *build_property(char *name, struct data val, struct property *build_property(const char *name, struct data val,
struct srcpos *srcpos); struct srcpos *srcpos);
struct property *build_property_delete(char *name); struct property *build_property_delete(const char *name);
struct property *chain_property(struct property *first, struct property *list); struct property *chain_property(struct property *first, struct property *list);
struct property *reverse_properties(struct property *first); struct property *reverse_properties(struct property *first);



2
fstree.c

@ -43,7 +43,7 @@ static struct node *read_fstree(const char *dirname)
"WARNING: Cannot open %s: %s\n", "WARNING: Cannot open %s: %s\n",
tmpname, strerror(errno)); tmpname, strerror(errno));
} else { } else {
prop = build_property(xstrdup(de->d_name), prop = build_property(de->d_name,
data_copy_file(pfile, data_copy_file(pfile,
st.st_size), st.st_size),
NULL); NULL);

8
livetree.c

@ -36,27 +36,27 @@ void delete_labels(struct label **labels)
label->deleted = 1; label->deleted = 1;
} }


struct property *build_property(char *name, struct data val, struct property *build_property(const char *name, struct data val,
struct srcpos *srcpos) struct srcpos *srcpos)
{ {
struct property *new = xmalloc(sizeof(*new)); struct property *new = xmalloc(sizeof(*new));


memset(new, 0, sizeof(*new)); memset(new, 0, sizeof(*new));


new->name = name; new->name = xstrdup(name);
new->val = val; new->val = val;
new->srcpos = srcpos_copy(srcpos); new->srcpos = srcpos_copy(srcpos);


return new; return new;
} }


struct property *build_property_delete(char *name) struct property *build_property_delete(const char *name)
{ {
struct property *new = xmalloc(sizeof(*new)); struct property *new = xmalloc(sizeof(*new));


memset(new, 0, sizeof(*new)); memset(new, 0, sizeof(*new));


new->name = name; new->name = xstrdup(name);
new->deleted = 1; new->deleted = 1;


return new; return new;

Loading…
Cancel
Save