From: Jonathan Nieder <jrnieder@gmail.com>
To: Jeff King <peff@peff.net>
Cc: Junio C Hamano <gitster@pobox.com>,
Lukas Fleischer <git@cryptocrack.de>,
git@vger.kernel.org
Subject: Re: [PATCH 1/5] git-compat-util: add xstrdup_or_null helper
Date: Mon, 12 Jan 2015 18:21:19 -0800 [thread overview]
Message-ID: <20150113022119.GA29365@google.com> (raw)
In-Reply-To: <20150113015736.GA18986@peff.net>
Jeff King wrote:
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -675,6 +675,11 @@ extern char *xgetcwd(void);
>
> #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x)))
>
> +static inline char *xstrdup_or_null(const char *str)
> +{
> + return str ? xstrdup(str) : NULL;
> +}
Would it make sense for xstrdup to always include the NULL check,
avoiding the need for the more verbose xstrdup_or_null?
Jonathan
diff --git i/wrapper.c w/wrapper.c
index 007ec0d..5a835e8 100644
--- i/wrapper.c
+++ w/wrapper.c
@@ -40,7 +40,11 @@ try_to_free_t set_try_to_free_routine(try_to_free_t routine)
char *xstrdup(const char *str)
{
- char *ret = strdup(str);
+ char *ret;
+
+ if (!str)
+ return NULL;
+ ret = strdup(str);
if (!ret) {
try_to_free_routine(strlen(str) + 1);
ret = strdup(str);
@@ -125,7 +129,11 @@ void *xmemdupz(const void *data, size_t len)
char *xstrndup(const char *str, size_t len)
{
- char *p = memchr(str, '\0', len);
+ char *p;
+
+ if (!str)
+ return NULL;
+ p = memchr(str, '\0', len);
return xmemdupz(str, p ? p - str : len);
}
next prev parent reply other threads:[~2015-01-13 2:21 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-01-10 21:33 [PATCH] blame.c: fix garbled error message Lukas Fleischer
2015-01-12 19:08 ` Junio C Hamano
2015-01-12 20:40 ` Jeff King
2015-01-12 22:55 ` Junio C Hamano
2015-01-12 23:12 ` Jeff King
2015-01-13 0:11 ` Junio C Hamano
2015-01-13 1:54 ` Jeff King
2015-01-13 1:57 ` [PATCH 1/5] git-compat-util: add xstrdup_or_null helper Jeff King
2015-01-13 2:21 ` Jonathan Nieder [this message]
2015-01-13 2:23 ` Jeff King
2015-01-13 1:58 ` [PATCH 2/5] builtin/apply.c: use xstrdup_or_null instead of null_strdup Jeff King
2015-01-13 1:58 ` [PATCH 3/5] builtin/commit.c: use xstrdup_or_null instead of envdup Jeff King
2015-01-13 1:59 ` [PATCH 4/5] use xstrdup_or_null to replace ternary conditionals Jeff King
2015-01-13 1:59 ` [PATCH 5/5] blame.c: fix garbled error message Jeff King
2015-01-14 14:21 ` [PATCH] " Lukas Fleischer
2015-01-14 17:22 ` Junio C Hamano
2015-01-14 20:49 ` Jeff King
2015-01-14 21:54 ` Junio C Hamano
2015-01-12 23:18 ` Lukas Fleischer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150113022119.GA29365@google.com \
--to=jrnieder@gmail.com \
--cc=git@cryptocrack.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.