]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
string: Overhaul para_strdup().
authorAndre Noll <maan@tuebingen.mpg.de>
Wed, 27 Oct 2021 17:14:03 +0000 (19:14 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Fri, 29 Jul 2022 17:22:05 +0000 (19:22 +0200)
We don't need to print an error message because it will be clear what
has happened when the assertion triggers.

Reword the documentation and mention that the memory allocated by
this function must be freed by the caller.

string.c

index f80331900de09fccf573c18666c3fa007f8ca6bf..cc6dc2e26227c59bdb04715dfb8b031f1a11e5d9 100644 (file)
--- a/string.c
+++ b/string.c
@@ -94,22 +94,23 @@ __must_check __malloc void *para_calloc(size_t size)
  *
  * \param s The string to be duplicated.
  *
- * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
- * there is no need to check the return value in the caller.
+ * A strdup(3)-like function which aborts if insufficient memory was available
+ * to allocate the duplicated string, absolving the caller from the
+ * responsibility to check for failure.
  *
- * \return A pointer to the duplicated string. If \a s was the \p NULL pointer,
- * an pointer to an empty string is returned.
+ * \return A pointer to the duplicated string. Unlike strdup(3), the caller may
+ * pass NULL, in which case the function returns a pointer to an empty string.
+ * Regardless of whether or not NULL was passed, the returned string is
+ * allocated on the heap and has to be freed by the caller.
  *
- * \sa strdup(3)
+ * \sa strdup(3).
  */
 __must_check __malloc char *para_strdup(const char *s)
 {
-       char *ret;
+       char *dupped_string = strdup(s? s: "");
 
-       if ((ret = strdup(s? s: "")))
-               return ret;
-       PARA_EMERG_LOG("strdup failed, aborting\n");
-       exit(EXIT_FAILURE);
+       assert(dupped_string);
+       return dupped_string;
 }
 
 /**