From: Ben Langmuir Date: Mon, 9 Feb 2015 21:55:44 +0000 (+0000) Subject: Be more conservative about gethostname()'s truncating behaviour X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c6957806f1a3c34b1c6838b387083eb65436f69a;p=clang Be more conservative about gethostname()'s truncating behaviour Don't assume it will provide an error or null-terminate the string on truncation, since POSIX doesn't guarantee either behaviour (although Linux and Darwin at least will do the 'right thing'). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@228613 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index e1d9351aa3..c1ad805420 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -2029,8 +2029,12 @@ std::string CompilerInvocation::getModuleHash() const { // running, so mangle the hostname in to the module hash to separate them. char hostname[256]; hostname[0] = 0; - if (gethostname(hostname, 255) == 0) + if (gethostname(hostname, 255) == 0) { + // Forcibly null-terminate the result, since POSIX doesn't require that + // truncation result in an error or that truncated names be null-terminated. + hostname[sizeof(hostname)-1] = 0; code = hash_combine(code, StringRef(hostname)); + } // Ignore failures in gethostname() by not including the hostname in the hash. #endif