]> diplodocus.org Git - nmh/blobdiff - sbr/getcpy.c
Fix invalid pointer arithmetic.
[nmh] / sbr / getcpy.c
index f23641dd81ec5326556a66e1e18d0d2dedf3ad5d..615f307f192d878779051dec383032d42e26e44a 100644 (file)
@@ -1,36 +1,22 @@
-
-/*
- * getcpy.c -- copy a string in managed memory
+/* getcpy.c -- copy a string in managed memory
  *
- * THIS IS OBSOLETE.  NEED TO REPLACE ALL OCCURENCES
+ * THIS IS OBSOLETE.  NEED TO REPLACE ALL OCCURRENCES
  * OF GETCPY WITH STRDUP.  BUT THIS WILL REQUIRE
  * CHANGING PARTS OF THE CODE TO DEAL WITH NULL VALUES.
  *
- * $Id$
- *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
  */
 
 #include <h/mh.h>
+#include <h/utils.h>
 
-
-char *
-getcpy (char *str)
+/* Return malloc'd copy of str, or of "" if NULL, exit on failure. */
+char *getcpy(const char *str)
 {
-    char *cp;
-    size_t len;
+    if (str)
+        return mh_xstrdup(str);
 
-    if (str) {
-       len = strlen(str) + 1;
-       if (!(cp = malloc (len)))
-           adios (NULL, "unable to allocate string storage");
-       memcpy (cp, str, len);
-    } else {
-       if (!(cp = malloc ((size_t) 1)))
-           adios (NULL, "unable to allocate string storage");
-       *cp = '\0';
-    }
-    return cp;
+    return mh_xcalloc(1, 1);
 }