Browse Source

Implement and use an xstrdup() function

Many places in dtc use strdup(), but none of them actually check the
return value to see if the implied allocation succeeded.  This is a
potential bug, which we fix in the patch below by replacing strdup()
with an xstrdup() which in analogy to xmalloc() will quit with a fatal
error if the allocation fails.

I felt the introduciton of util.[ch] was a better choice
for utility oriented code than directly using srcpos.c
for the new string function.

This patch is a re-factoring of Dave Gibson's similar patch.

Signed-off-by: Jon Loeliger <jdl@freescale.com>
main
Jon Loeliger 16 years ago
parent
commit
879e4d2590
  1. 3
      Makefile.convert-dtsv0
  2. 13
      Makefile.dtc
  3. 3
      convert-dtsv0-lexer.l
  4. 14
      dtc-lexer.l
  5. 3
      dtc.h
  6. 6
      flattree.c
  7. 4
      fstree.c
  8. 4
      srcpos.c
  9. 30
      util.c
  10. 20
      util.h

3
Makefile.convert-dtsv0

@ -5,7 +5,8 @@ @@ -5,7 +5,8 @@
#

CONVERT_SRCS = \
srcpos.c
srcpos.c \
util.c

CONVERT_GEN_SRCS = convert-dtsv0-lexer.lex.c


13
Makefile.dtc

@ -3,7 +3,16 @@ @@ -3,7 +3,16 @@
# This is not a complete Makefile of itself. Instead, it is designed to
# be easily embeddable into other systems of Makefiles.
#
DTC_SRCS = dtc.c flattree.c fstree.c data.c livetree.c treesource.c srcpos.c \
checks.c
DTC_SRCS = \
checks.c \
data.c \
dtc.c \
flattree.c \
fstree.c \
livetree.c \
srcpos.c \
treesource.c \
util.c

DTC_GEN_SRCS = dtc-lexer.lex.c dtc-parser.tab.c
DTC_OBJS = $(DTC_SRCS:%.c=%.o) $(DTC_GEN_SRCS:%.c=%.o)

3
convert-dtsv0-lexer.l

@ -42,6 +42,7 @@ GAP ({WS}|{COMMENT}|{LINECOMMENT})* @@ -42,6 +42,7 @@ GAP ({WS}|{COMMENT}|{LINECOMMENT})*
#include <fnmatch.h>

#include "srcpos.h"
#include "util.h"

static int v1_tagged; /* = 0 */
static int cbase = 16;
@ -185,7 +186,7 @@ const struct { @@ -185,7 +186,7 @@ const struct {

<PROPNODENAME>{PROPNODECHAR}+ {
ECHO;
last_name = strdup(yytext);
last_name = xstrdup(yytext);
BEGIN(INITIAL);
}


14
dtc-lexer.l

@ -105,7 +105,7 @@ static int pop_input_file(void); @@ -105,7 +105,7 @@ static int pop_input_file(void);
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
DPRINT("Label: %s\n", yytext);
yylval.labelref = strdup(yytext);
yylval.labelref = xstrdup(yytext);
yylval.labelref[yyleng-1] = '\0';
return DT_LABEL;
}
@ -128,7 +128,7 @@ static int pop_input_file(void); @@ -128,7 +128,7 @@ static int pop_input_file(void);
<INITIAL>[0-9a-fA-F]+ {
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
yylval.literal = strdup(yytext);
yylval.literal = xstrdup(yytext);
DPRINT("Literal: '%s'\n", yylval.literal);
return DT_LEGACYLITERAL;
}
@ -136,7 +136,7 @@ static int pop_input_file(void); @@ -136,7 +136,7 @@ static int pop_input_file(void);
<V1>[0-9]+|0[xX][0-9a-fA-F]+ {
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
yylval.literal = strdup(yytext);
yylval.literal = xstrdup(yytext);
DPRINT("Literal: '%s'\n", yylval.literal);
return DT_LITERAL;
}
@ -145,7 +145,7 @@ static int pop_input_file(void); @@ -145,7 +145,7 @@ static int pop_input_file(void);
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
DPRINT("Ref: %s\n", yytext+1);
yylval.labelref = strdup(yytext+1);
yylval.labelref = xstrdup(yytext+1);
return DT_REF;
}

@ -154,7 +154,7 @@ static int pop_input_file(void); @@ -154,7 +154,7 @@ static int pop_input_file(void);
yylloc.first_line = yylineno;
yytext[yyleng-1] = '\0';
DPRINT("Ref: %s\n", yytext+2);
yylval.labelref = strdup(yytext+2);
yylval.labelref = xstrdup(yytext+2);
return DT_REF;
}

@ -162,7 +162,7 @@ static int pop_input_file(void); @@ -162,7 +162,7 @@ static int pop_input_file(void);
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
DPRINT("Ref: %s\n", yytext+1);
yylval.labelref = strdup(yytext+1);
yylval.labelref = xstrdup(yytext+1);
return DT_REF;
}

@ -186,7 +186,7 @@ static int pop_input_file(void); @@ -186,7 +186,7 @@ static int pop_input_file(void);
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
DPRINT("PropNodeName: %s\n", yytext);
yylval.propnodename = strdup(yytext);
yylval.propnodename = xstrdup(yytext);
BEGIN_DEFAULT();
return DT_PROPNODENAME;
}

3
dtc.h

@ -34,7 +34,10 @@ @@ -34,7 +34,10 @@
#include <libfdt_env.h>
#include <fdt.h>

#include "util.h"

#define DEFAULT_FDT_VERSION 17

/*
* Command line options
*/

6
flattree.c

@ -601,7 +601,7 @@ static char *flat_read_string(struct inbuf *inb) @@ -601,7 +601,7 @@ static char *flat_read_string(struct inbuf *inb)
len++;
} while ((*p++) != '\0');

str = strdup(inb->ptr);
str = xstrdup(inb->ptr);

inb->ptr += len;

@ -643,7 +643,7 @@ static char *flat_read_stringtable(struct inbuf *inb, int offset) @@ -643,7 +643,7 @@ static char *flat_read_stringtable(struct inbuf *inb, int offset)
p++;
}

return strdup(inb->base + offset);
return xstrdup(inb->base + offset);
}

static struct property *flat_read_property(struct inbuf *dtbuf,
@ -710,7 +710,7 @@ static char *nodename_from_path(const char *ppath, const char *cpath) @@ -710,7 +710,7 @@ static char *nodename_from_path(const char *ppath, const char *cpath)
if (!streq(ppath, "/"))
plen++;

return strdup(cpath + plen);
return xstrdup(cpath + plen);
}

static struct node *unflatten_tree(struct inbuf *dtbuf,

4
fstree.c

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

newchild = read_fstree(tmpnam);
newchild = name_node(newchild, strdup(de->d_name),
newchild = name_node(newchild, xstrdup(de->d_name),
NULL);
add_child(tree, newchild);
}

4
srcpos.c

@ -39,7 +39,7 @@ static int dtc_open_one(struct dtc_file *file, @@ -39,7 +39,7 @@ static int dtc_open_one(struct dtc_file *file,
strcat(fullname, "/");
strcat(fullname, fname);
} else {
fullname = strdup(fname);
fullname = xstrdup(fname);
}

file->file = fopen(fullname, "r");
@ -85,7 +85,7 @@ struct dtc_file *dtc_open_file(const char *fname, @@ -85,7 +85,7 @@ struct dtc_file *dtc_open_file(const char *fname,
if (!file->file)
goto fail;

file->name = strdup(fname);
file->name = xstrdup(fname);
return file;
}


30
util.c

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
/*
* Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/

#include "dtc.h"

char *xstrdup(const char *s)
{
int len = strlen(s) + 1;
char *dup = xmalloc(len);

memcpy(dup, s, len);

return dup;
}

20
util.h

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
/*
* Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/

extern char *xstrdup(const char *s);
Loading…
Cancel
Save