From: Andre Noll Date: Wed, 27 Oct 2021 17:14:03 +0000 (+0200) Subject: string: Overhaul para_strdup(). X-Git-Tag: v0.7.1~1^2~6 X-Git-Url: http://git.tuebingen.mpg.de/?a=commitdiff_plain;h=89e0813c310816e05d22c0bfab7b1be07b3fc841;p=paraslash.git string: Overhaul para_strdup(). 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. --- diff --git a/string.c b/string.c index f8033190..cc6dc2e2 100644 --- 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; } /**