From: Guus Sliepen Date: Fri, 27 Sep 2013 07:43:39 +0000 (+0200) Subject: Fix segfault when Name = $HOST but $HOST is not set. X-Git-Tag: release-1.0.23~6 X-Git-Url: https://git.tinc-vpn.org/git/browse?a=commitdiff_plain;h=ef24d3997fe83e85dd1b4e9a164a3d96340cca7e;p=tinc Fix segfault when Name = $HOST but $HOST is not set. --- diff --git a/src/net_setup.c b/src/net_setup.c index 623ac383..e1d9f275 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -295,17 +295,18 @@ char *get_name(void) { if(*name == '$') { char *envname = getenv(name + 1); + char hostname[32] = ""; if(!envname) { if(strcmp(name + 1, "HOST")) { fprintf(stderr, "Invalid Name: environment variable %s does not exist\n", name + 1); return false; } - char envname[32]; - if(gethostname(envname, 32)) { + if(gethostname(hostname, sizeof hostname) || !*hostname) { fprintf(stderr, "Could not get hostname: %s\n", strerror(errno)); return false; } - envname[31] = 0; + hostname[31] = 0; + envname = hostname; } free(name); name = xstrdup(envname);