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/misc.c | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) (limited to 'src/misc.c') 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; } -- cgit v1.2.3