diff options
author | Yang Tse <yangsita@gmail.com> | 2011-10-13 23:00:24 +0200 |
---|---|---|
committer | Yang Tse <yangsita@gmail.com> | 2011-10-13 23:00:24 +0200 |
commit | ea12c72d122cb35548979fb86dcec7f1488fc5d6 (patch) | |
tree | 18a36ddb33dfc87e7c806e260b226c3332455d01 /lib/curl_gethostname.c | |
parent | 47e4537ac6d7c8b40ba742908efd6ddb1d362ba9 (diff) | |
download | curl-ea12c72d122cb35548979fb86dcec7f1488fc5d6.tar.gz |
curl_gethostname.c: fix signed/unsigned comparison and avoid a double copy
both introduced in 42be24af
Diffstat (limited to 'lib/curl_gethostname.c')
-rw-r--r-- | lib/curl_gethostname.c | 39 |
1 files changed, 17 insertions, 22 deletions
diff --git a/lib/curl_gethostname.c b/lib/curl_gethostname.c index 6fcfdc542..aafa45b59 100644 --- a/lib/curl_gethostname.c +++ b/lib/curl_gethostname.c @@ -62,46 +62,41 @@ int Curl_gethostname(char *name, GETHOSTNAME_TYPE_ARG2 namelen) { return -1; #else - int err = 0; - char* dot = NULL; - char hostname[HOSTNAME_MAX + 1]; + int err; + char* dot; #ifdef DEBUGBUILD /* Override host name when environment variable CURL_GETHOSTNAME is set */ const char *force_hostname = getenv("CURL_GETHOSTNAME"); if(force_hostname) { - strncpy(hostname, force_hostname, sizeof(hostname)); - hostname[sizeof(hostname) - 1] = '\0'; + strncpy(name, force_hostname, namelen); + err = 0; + } + else { + name[0] = '\0'; + err = gethostname(name, namelen); } - else - err = gethostname(hostname, sizeof(hostname)); #else /* DEBUGBUILD */ /* The call to system's gethostname() might get intercepted by the libhostname library when libcurl is built as a non-debug shared library when running the test suite. */ - err = gethostname(hostname, sizeof(hostname)); + name[0] = '\0'; + err = gethostname(name, namelen); #endif - if(err != 0) + name[namelen - 1] = '\0'; + + if(err) return err; - /* Is the hostname fully qualified? */ - dot = strchr(hostname, '.'); - if(dot) { - /* Copy only the machine name to the specified buffer */ - size_t size = dot - hostname; - strncpy(name, hostname, namelen > size ? size : namelen); - name[(namelen > size ? size : namelen) - 1] = '\0'; - } - else { - /* Copy the hostname to the specified buffer */ - strncpy(name, hostname, namelen); - name[namelen - 1] = '\0'; - } + /* Truncate domain, leave only machine name */ + dot = strchr(name, '.'); + if(dot) + *dot = '\0'; return 0; #endif |