【发布时间】:2013-09-20 05:13:32
【问题描述】:
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 给我带来了最大的麻烦)?由于苹果的内部结构取决于它,它甚至可以做到吗?
【问题讨论】:
-
你来自 C 语言,对吧?在 C++ 中没有必要指定一个元素是一个结构:
const struct hostent &s应该是const hostent &s -
另外,赋值运算符的正确行为是返回对对象的引用
-
感谢 Manu343726。上面的代码是存在的。不知道它在那里多久了。我担心的是未对齐访问的运行时错误,而不是语言问题。
-
如果它实际上没有触发 SIGBUS,您不必担心 SIGBUS,实际上在 x64 和类似 CPU 上它不会触发任何 SIGBUS。 IE。您确实可以忽略 UBSan 运行时警告。如果您移植到可能导致 SIGBUS 的平台,那么该平台上的相关
libc函数可能不会生成具有此类对齐问题的struct hostent对象,否则访问它们的任何东西都会崩溃。
标签: c++ macos pointers alignment