Devicetree
 help / color / mirror / Atom feed
From: David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>
To: Jon Loeliger <jdl-KZfg59tc24xl57MIdRCFDg@public.gmane.org>
Cc: devicetree-discuss-mnsaURCQ41sdnm+yROfE0A@public.gmane.org
Subject: [1/5] dtc: Implement and use an xstrdup() function
Date: Fri, 3 Oct 2008 00:05:12 +1000	[thread overview]
Message-ID: <20081002140512.GE11662@yookeroo.seuss> (raw)
In-Reply-To: <20081002140427.GD11662-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>

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.

xstrdup() is defined in srcpos.c, because that's available to both dtc
itself and the conversion program which also uses it.  While we're at
it, we add standard double-include protection to srcpos.h which was
missing it.

Signed-off-by: David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>

---
 convert-dtsv0-lexer.l |    2 +-
 dtc-lexer.l           |   15 +++++++--------
 dtc-parser.y          |    1 -
 dtc.c                 |    1 -
 dtc.h                 |    2 ++
 flattree.c            |    7 +++----
 fstree.c              |    4 ++--
 srcpos.c              |   13 +++++++++++--
 srcpos.h              |    6 ++++++
 treesource.c          |    1 -
 10 files changed, 32 insertions(+), 20 deletions(-)

Index: dtc/convert-dtsv0-lexer.l
===================================================================
--- dtc.orig/convert-dtsv0-lexer.l	2008-10-03 00:03:36.000000000 +1000
+++ dtc/convert-dtsv0-lexer.l	2008-10-03 00:03:38.000000000 +1000
@@ -185,7 +185,7 @@ const struct {
 
 <PROPNODENAME>{PROPNODECHAR}+ {
 			ECHO;
-			last_name = strdup(yytext);
+			last_name = xstrdup(yytext);
 			BEGIN(INITIAL);
 		}
 
Index: dtc/dtc-lexer.l
===================================================================
--- dtc.orig/dtc-lexer.l	2008-10-03 00:03:36.000000000 +1000
+++ dtc/dtc-lexer.l	2008-10-03 00:03:38.000000000 +1000
@@ -35,7 +35,6 @@ LINECOMMENT	"//".*\n
 
 %{
 #include "dtc.h"
-#include "srcpos.h"
 #include "dtc-parser.tab.h"
 
 
@@ -105,7 +104,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 +127,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 +135,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 +144,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 +153,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 +161,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 +185,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;
 		}
Index: dtc/dtc-parser.y
===================================================================
--- dtc.orig/dtc-parser.y	2008-10-03 00:03:36.000000000 +1000
+++ dtc/dtc-parser.y	2008-10-03 00:03:38.000000000 +1000
@@ -24,7 +24,6 @@
 #include <stdio.h>
 
 #include "dtc.h"
-#include "srcpos.h"
 
 extern int yylex(void);
 
Index: dtc/dtc.c
===================================================================
--- dtc.orig/dtc.c	2008-10-03 00:03:36.000000000 +1000
+++ dtc/dtc.c	2008-10-03 00:03:38.000000000 +1000
@@ -19,7 +19,6 @@
  */
 
 #include "dtc.h"
-#include "srcpos.h"
 
 #include "version_gen.h"
 
Index: dtc/dtc.h
===================================================================
--- dtc.orig/dtc.h	2008-10-03 00:03:36.000000000 +1000
+++ dtc/dtc.h	2008-10-03 00:03:38.000000000 +1000
@@ -34,6 +34,8 @@
 #include <libfdt_env.h>
 #include <fdt.h>
 
+#include "srcpos.h"
+
 #define DEFAULT_FDT_VERSION	17
 /*
  * Command line options
Index: dtc/flattree.c
===================================================================
--- dtc.orig/flattree.c	2008-10-03 00:03:36.000000000 +1000
+++ dtc/flattree.c	2008-10-03 00:03:38.000000000 +1000
@@ -19,7 +19,6 @@
  */
 
 #include "dtc.h"
-#include "srcpos.h"
 
 #define FTF_FULLPATH	0x1
 #define FTF_VARALIGN	0x2
@@ -601,7 +600,7 @@ static char *flat_read_string(struct inb
 		len++;
 	} while ((*p++) != '\0');
 
-	str = strdup(inb->ptr);
+	str = xstrdup(inb->ptr);
 
 	inb->ptr += len;
 
@@ -643,7 +642,7 @@ static char *flat_read_stringtable(struc
 		p++;
 	}
 
-	return strdup(inb->base + offset);
+	return xstrdup(inb->base + offset);
 }
 
 static struct property *flat_read_property(struct inbuf *dtbuf,
@@ -710,7 +709,7 @@ static char *nodename_from_path(const ch
 	if (!streq(ppath, "/"))
 		plen++;
 
-	return strdup(cpath + plen);
+	return xstrdup(cpath + plen);
 }
 
 static struct node *unflatten_tree(struct inbuf *dtbuf,
Index: dtc/fstree.c
===================================================================
--- dtc.orig/fstree.c	2008-10-03 00:03:36.000000000 +1000
+++ dtc/fstree.c	2008-10-03 00:03:38.000000000 +1000
@@ -58,7 +58,7 @@ static struct node *read_fstree(const ch
 					"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 ch
 			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);
 		}
Index: dtc/srcpos.c
===================================================================
--- dtc.orig/srcpos.c	2008-10-03 00:03:36.000000000 +1000
+++ dtc/srcpos.c	2008-10-03 00:03:38.000000000 +1000
@@ -20,6 +20,15 @@
 #include "dtc.h"
 #include "srcpos.h"
 
+char *xstrdup(const char *s)
+{
+	int len = strlen(s);
+	char *dup = xmalloc(len + 1);
+
+	memcpy(dup, s, len+1);
+	return dup;
+}
+
 /*
  * Like yylineno, this is the current open file pos.
  */
@@ -39,7 +48,7 @@ static int dtc_open_one(struct dtc_file 
 		strcat(fullname, "/");
 		strcat(fullname, fname);
 	} else {
-		fullname = strdup(fname);
+		fullname = xstrdup(fname);
 	}
 
 	file->file = fopen(fullname, "r");
@@ -85,7 +94,7 @@ struct dtc_file *dtc_open_file(const cha
 		if (!file->file)
 			goto fail;
 
-		file->name = strdup(fname);
+		file->name = xstrdup(fname);
 		return file;
 	}
 
Index: dtc/srcpos.h
===================================================================
--- dtc.orig/srcpos.h	2008-10-03 00:03:36.000000000 +1000
+++ dtc/srcpos.h	2008-10-03 00:03:38.000000000 +1000
@@ -1,3 +1,5 @@
+#ifndef _SRCPOS_H
+#define _SRCPOS_H
 /*
  * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
  *
@@ -24,6 +26,8 @@
 
 #include <stdio.h>
 
+char *xstrdup(const char *s);
+
 struct dtc_file {
 	char *dir;
 	const char *name;
@@ -83,3 +87,5 @@ struct search_path {
 extern struct dtc_file *dtc_open_file(const char *fname,
                                       const struct search_path *search);
 extern void dtc_close_file(struct dtc_file *file);
+
+#endif /* _SRCPOS_H */
Index: dtc/treesource.c
===================================================================
--- dtc.orig/treesource.c	2008-10-03 00:03:36.000000000 +1000
+++ dtc/treesource.c	2008-10-03 00:03:38.000000000 +1000
@@ -19,7 +19,6 @@
  */
 
 #include "dtc.h"
-#include "srcpos.h"
 
 extern FILE *yyin;
 extern int yyparse(void);


-- 
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

  parent reply	other threads:[~2008-10-02 14:05 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-02 14:04 [0/5] dtc: srcpos, input handling cleanups David Gibson
     [not found] ` <20081002140427.GD11662-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
2008-10-02 14:05   ` David Gibson [this message]
     [not found]     ` <20081002140512.GE11662-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
2008-10-02 14:05       ` [2/5] dtc: Use flex's YY_USER_ACTION feature to avoid code duplication David Gibson
     [not found]         ` <20081002140556.GF11662-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
2008-10-02 14:06           ` [3/5] dtc: Cleanup yyerrorf() function David Gibson
     [not found]             ` <20081002140652.GG11662-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
2008-10-02 14:07               ` [4/5] dtc: Cleanup yylloc type and handling David Gibson
     [not found]                 ` <20081002140753.GH11662-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
2008-10-02 14:09                   ` [5/5] dtc: Clean up source file management David Gibson
2008-10-03 19:24                   ` [4/5] dtc: Cleanup yylloc type and handling Jon Loeliger
     [not found]                     ` <E1KlqGI-0006JF-6p-CYoMK+44s/E@public.gmane.org>
2008-10-04  2:25                       ` David Gibson
2008-10-03 19:22               ` [3/5] dtc: Cleanup yyerrorf() function Jon Loeliger
     [not found]                 ` <E1KlqEb-0006Io-Sc-CYoMK+44s/E@public.gmane.org>
2008-10-04  2:56                   ` David Gibson
2008-10-02 16:25           ` [2/5] dtc: Use flex's YY_USER_ACTION feature to avoid code duplication Jon Loeliger
2008-10-03  1:05             ` David Gibson
     [not found]               ` <20081003010531.GE3002-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
2008-10-03 14:17                 ` Jon Loeliger
     [not found]                   ` <48E62991.6010102-KZfg59tc24xl57MIdRCFDg@public.gmane.org>
2008-10-04  4:13                     ` David Gibson
2008-10-03 17:16           ` Jon Loeliger
2008-10-03 17:17       ` [1/5] dtc: Implement and use an xstrdup() function Jon Loeliger
     [not found]         ` <E1KloHP-0005pb-R8-CYoMK+44s/E@public.gmane.org>
2008-10-04  2:49           ` David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20081002140512.GE11662@yookeroo.seuss \
    --to=david-xt8fgy+axnrb3ne2bgzf6laj5h9x9tb+@public.gmane.org \
    --cc=devicetree-discuss-mnsaURCQ41sdnm+yROfE0A@public.gmane.org \
    --cc=jdl-KZfg59tc24xl57MIdRCFDg@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox