<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 9, 2015 at 12:13 PM, Ben Langmuir <span dir="ltr"><<a href="mailto:blangmuir@apple.com" target="_blank">blangmuir@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div id=":3ik" class="a3s" style="overflow:hidden">Date: Mon Feb  9 14:13:11 2015<br>
New Revision: 228601<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=228601&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=228601&view=rev</a><br>
Log:<br>
Update r228592 for when gethostname() returns an error<br>
<br>
If gethostname() is not successful, just skip adding the hostname to the<br>
module hash.  And don't bother setting hostname[255] = 0, since if<br>
gethostname() is successful, it will be null-terminated already (and if<br>
it's not successful we don't read the string now.<br></div></blockquote></div><br>Sadly, I think this still isn't enough to be pedantically correct.</div><div class="gmail_extra"><br></div><div class="gmail_extra">The relevant specs allow for a gethostname to not return an error even when truncation occurs, and do not require null termination when truncation occurs. Linux (glibc at least) returns an error and set errno to either EINVAL or ENAMETOOLONG. SUS and POSIX don't really seem to allow this to fail...</div><div class="gmail_extra"><br></div><div class="gmail_extra">I think the "best" approach here is:</div><div class="gmail_extra"><br></div><div class="gmail_extra">if (gethostname(...) == 0 || errno == ENAMETOOLONG || errno == EINVAL) {<br>  hostname[sizeof(hostname) - 1] = 0;</div><div class="gmail_extra">  hash_combine(...);</div><div class="gmail_extra">}</div><div class="gmail_extra"><br></div><div class="gmail_extra">But I'm fine if you refuse to handle ENAMETOOLONG or EINVAL, and just null terminate the thing forcibly and only use it when it returns a non-error. I don't believe it is possible for you to reach the error on Linux because the kernel doesn't provide 256 bytes for the hostname anyways. ;]</div></div>