From 399e94f904b913a4093295426820ab89fc3cd24f Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Wed, 2 Dec 2015 11:32:42 -0500 Subject: Made string copying more standard. copy_string() was a clone of the stdlib's strdup(). For safety, simplicity, and speed, we should use that instead. We introduce xstrdup() which wraps strdup() in a failure upon memory allocation errors. --- src/flexdef.h | 4 ++-- src/misc.c | 26 ++++++-------------------- src/parse.y | 12 ++++++------ src/scan.l | 6 +++--- src/sym.c | 4 ++-- src/tables.c | 4 ++-- 6 files changed, 21 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/flexdef.h b/src/flexdef.h index ede6a93..066f804 100644 --- a/src/flexdef.h +++ b/src/flexdef.h @@ -857,8 +857,8 @@ extern void check_char PROTO ((int c)); /* Replace upper-case letter to lower-case. */ extern unsigned char clower PROTO ((int)); -/* Returns a dynamically allocated copy of a string. */ -extern char *copy_string PROTO ((const char *)); +/* strdup() that fails fatally on allocation failures. */ +extern char *xstrdup(const char *); /* Returns a dynamically allocated copy of a (potentially) unsigned string. */ extern unsigned char *copy_unsigned_string PROTO ((unsigned char *)); diff --git a/src/misc.c b/src/misc.c index 2bffdcd..d973b24 100644 --- a/src/misc.c +++ b/src/misc.c @@ -106,7 +106,7 @@ void action_define (const char *defname, int value) add_action (buf); /* track #defines so we can undef them when we're done. */ - cpy = copy_string (defname); + cpy = xstrdup(defname); buf_append (&defs_buf, &cpy, 1); } @@ -240,28 +240,14 @@ unsigned char clower (int c) } -/* copy_string - returns a dynamically allocated copy of a string */ - -char *copy_string (const char *str) +char *xstrdup(const char *s) { - const char *c1; - char *c2; - char *copy; - unsigned int size; - - /* find length */ - for (c1 = str; *c1; ++c1) ; - - size = (c1 - str + 1) * sizeof (char); + char *s2; - copy = (char *) flex_alloc (size); + if ((s2 = strdup(s)) == NULL) + flexfatal (_("memory allocation failure in xstrdup()")); - if (copy == NULL) - flexfatal (_("dynamic memory failure in copy_string()")); - - for (c2 = copy; (*c2++ = *str++) != 0;) ; - - return copy; + return s2; } diff --git a/src/parse.y b/src/parse.y index 6e7246c..9bec1ee 100644 --- a/src/parse.y +++ b/src/parse.y @@ -193,19 +193,19 @@ optionlist : optionlist option option : OPT_OUTFILE '=' NAME { - outfilename = copy_string( nmstr ); + outfilename = xstrdup(nmstr); did_outfilename = 1; } | OPT_EXTRA_TYPE '=' NAME - { extra_type = copy_string( nmstr ); } + { extra_type = xstrdup(nmstr); } | OPT_PREFIX '=' NAME - { prefix = copy_string( nmstr ); } + { prefix = xstrdup(nmstr); } | OPT_YYCLASS '=' NAME - { yyclass = copy_string( nmstr ); } + { yyclass = xstrdup(nmstr); } | OPT_HEADER '=' NAME - { headerfilename = copy_string( nmstr ); } + { headerfilename = xstrdup(nmstr); } | OPT_TABLES '=' NAME - { tablesext = true; tablesfilename = copy_string( nmstr ); } + { tablesext = true; tablesfilename = xstrdup(nmstr); } ; sect2 : sect2 scon initforrule flexrule '\n' diff --git a/src/scan.l b/src/scan.l index bc316be..4a96f23 100644 --- a/src/scan.l +++ b/src/scan.l @@ -232,7 +232,7 @@ M4QEND "]]" \"[^"\n]*\" { flex_free( (void *) infilename ); - infilename = copy_string( yytext + 1 ); + infilename = xstrdup(yytext + 1); infilename[strlen( infilename ) - 1] = '\0'; } . /* ignore spurious characters */ @@ -994,7 +994,7 @@ void set_input_file( char *file ) { if ( file && strcmp( file, "-" ) ) { - infilename = copy_string( file ); + infilename = xstrdup(file); yyin = fopen( infilename, "r" ); if ( yyin == NULL ) @@ -1004,7 +1004,7 @@ void set_input_file( char *file ) else { yyin = stdin; - infilename = copy_string( "" ); + infilename = xstrdup(""); } linenum = 1; diff --git a/src/sym.c b/src/sym.c index 568a24b..e6f556c 100644 --- a/src/sym.c +++ b/src/sym.c @@ -181,7 +181,7 @@ static int hashfunct (const char *str, int hash_size) void ndinstal (const char *name, unsigned char definition[]) { - if (addsym (copy_string (name), + if (addsym (xstrdup(name), (char *) copy_unsigned_string (definition), 0, ndtbl, NAME_TABLE_HASH_SIZE)) synerr (_("name defined twice")); @@ -227,7 +227,7 @@ void scinstal (const char *str, int xcluflg) if (++lastsc >= current_max_scs) scextend (); - scname[lastsc] = copy_string (str); + scname[lastsc] = xstrdup(str); if (addsym (scname[lastsc], (char *) 0, lastsc, sctbl, START_COND_HASH_SIZE)) diff --git a/src/tables.c b/src/tables.c index ef49ad0..62a9dcc 100644 --- a/src/tables.c +++ b/src/tables.c @@ -90,8 +90,8 @@ int yytbl_hdr_init (struct yytbl_hdr *th, const char *version_str, th->th_hsize += yypad64 (th->th_hsize); th->th_ssize = 0; // Not known at this point. th->th_flags = 0; - th->th_version = copy_string (version_str); - th->th_name = copy_string (name); + th->th_version = xstrdup(version_str); + th->th_name = xstrdup(name); return 0; } -- cgit v1.2.3