summaryrefslogtreecommitdiff
path: root/ident.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2016-09-23 00:37:53 -0400
committerJunio C Hamano <gitster@pobox.com>2016-09-23 10:01:15 -0700
commitc375a7efa3a42afc51385659437f04027ed106b1 (patch)
tree468d44656a0f9a68e1ad431e1e3dacef45bd80ea /ident.c
parent0b65a8dbdb38962e700ee16776a3042beb489060 (diff)
downloadgit-c375a7efa3a42afc51385659437f04027ed106b1.tar.gz
ident: handle NULL ai_canonnamejk/ident-ai-canonname-could-be-null
We call getaddrinfo() to try to convert a short hostname into a fully-qualified one (to use it as an email domain). If there isn't a canonical name, getaddrinfo() will generally return either a NULL addrinfo list, or one in which ai->ai_canonname is a copy of the original name. However, if the result of gethostname() looks like an IP address, then getaddrinfo() behaves differently on some systems. On OS X, it will return a "struct addrinfo" with a NULL ai_canonname, and we segfault feeding it to strchr(). This is hard to test reliably because it involves not only a system where we we have to fallback to gethostname() to come up with an ident, but also where the hostname is a number with no dots. But I was able to replicate the bug by faking a hostname, like: diff --git a/ident.c b/ident.c index e20a772..b790d28 100644 --- a/ident.c +++ b/ident.c @@ -128,6 +128,7 @@ static void add_domainname(struct strbuf *out, int *is_bogus) *is_bogus = 1; return; } + xsnprintf(buf, sizeof(buf), "1"); if (strchr(buf, '.')) strbuf_addstr(out, buf); else if (canonical_name(buf, out) < 0) { and running "git var GIT_AUTHOR_IDENT" on an OS X system. Before this patch it segfaults, and after we correctly complain of the bogus "user@1.(none)" address (though this bogus address would be suitable for non-object uses like writing reflogs). Reported-by: Jonas Thiel <jonas.lierschied@gmx.de> Diagnosed-by: John Keeping <john@keeping.me.uk> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'ident.c')
-rw-r--r--ident.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/ident.c b/ident.c
index 4fd82d1043..28bc854918 100644
--- a/ident.c
+++ b/ident.c
@@ -103,7 +103,7 @@ static int canonical_name(const char *host, struct strbuf *out)
memset (&hints, '\0', sizeof (hints));
hints.ai_flags = AI_CANONNAME;
if (!getaddrinfo(host, NULL, &hints, &ai)) {
- if (ai && strchr(ai->ai_canonname, '.')) {
+ if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) {
strbuf_addstr(out, ai->ai_canonname);
status = 0;
}