r228613 - Be more conservative about gethostname()'s truncating behaviour

Ben Langmuir blangmuir at apple.com
Mon Feb 9 13:55:44 PST 2015


Author: benlangmuir
Date: Mon Feb  9 15:55:44 2015
New Revision: 228613

URL: http://llvm.org/viewvc/llvm-project?rev=228613&view=rev
Log:
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').

Modified:
    cfe/trunk/lib/Frontend/CompilerInvocation.cpp

Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=228613&r1=228612&r2=228613&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Mon Feb  9 15:55:44 2015
@@ -2029,8 +2029,12 @@ std::string CompilerInvocation::getModul
   // 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
 





More information about the cfe-commits mailing list