【发布时间】:2021-08-22 23:00:19
【问题描述】:
#include <stdio.h>
#include <unistd.h>
int main (int argc, char *argv[]) {
char buf[4];
int err = gethostname(buf, 4);
printf("name: %s n: %d\n", buf, err); // BUG: err should be -1, and errno should give ENAMETOOLONG
return 0;
}
在 OSX 上,这给出:
name: xyz n: 0
即使我的主机名大于 3;相反,我希望 err 为 -1 而 errno 为 ENAMETOOLONG;
请注意,如果用户不知道此错误,并且在缓冲区太短时预计 err 为 -1(文档说 \0 未写入缓冲区),这也可能导致缓冲区溢出错误如果缓冲区太短),因此即使 err == 0,上述 printf 也是不合理的。我在调查 https://github.com/nim-lang/Nim/issues/18088 时观察到了这一点。
这是一个错误吗?如果有,在哪里举报?
【问题讨论】:
-
您误解了
gethostname函数的规范。再读一遍。 -
你如何解释这个?
[ENAMETOOLONG] The current host name is longer than namelen. (For gethostname() only.) -
什么意思?我没有看到与文档相矛盾的消息。
-
我的主机名长度为 23;我正在传递
namelen = 4,它 -
可能是文档错误。 linux equivalent 的行为不同。