Clang 3.3 UBSan(未定义的行为清理程序)将以下代码标记为未对齐访问:
Address.cc:545:27: runtime error: load of misaligned address 0x60c00000a7df for type 'char *', which requires 8 byte alignment
0x60c00000a7df: note: pointer points here
00 00 00 00 ef a7 00 00 c0 60 00 00 00 00 00 00 00 00 00 00 c0 a8 64 0c 00 00 00 00 00 00 00 00
^
Address.cc:547:19: runtime error: reference binding to misaligned address 0x60c00000a7ef for type 'const struct in_addr', which requires 4 byte alignment
0x60c00000a7ef: note: pointer points here
00 00 00 00 c0 a8 64 0c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^
有问题的代码如下所示(忽略不正确的返回类型operator=
):
bool
Ip::Address::operator =(const struct hostent &s)
{
struct in_addr* ipv4 = NULL;
switch (s.h_addrtype) {
case AF_INET:
// Line 545 below
ipv4 = (in_addr*)(s.h_addr_list[0]);
// Line 547 below
operator=(*ipv4);
break;
...
}
和hostent
结构:
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses */
}
这个squawk 是在Mac OS X 系统上产生的,我记得最近关于这是Xcode 或CFE Dev 邮件列表上的一个错误的讨论(但我现在找不到它)。
编辑:Sean McBride 很友好地提供了电子邮件的链接:-fcatch-undefined-behavior false positive with readdir()? 我不同意可以忽略它的说法(因为它来自操作系统而不是应用程序),特别是因为它可能导致SIGBUS
错误。
如何重新对齐指针以清除 Clang 问题(h_addr
给我带来最大的麻烦)?由于Apple的内部结构取决于它,它甚至可以完成吗?