如何修复未对齐指针(字符**) [英] How to fix unaligned pointer (char**)

查看:5267
本文介绍了如何修复未对齐指针(字符**)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

锵3.3 UBSan(消毒剂未定义行为)标记以下$ C $下对齐访问:

Clang 3.3 UBSan (undefined behavior sanitizer) flagged the following code for unaligned access:

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
             ^

在code问题如下所示(忽略不正确的返回类型操作符= ):

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 */
}

这发牢骚制作在Mac OS X系统,我记得这个是在x code或CFE开发邮件列表中的错误(但我不能在此刻找到它)最近的讨论。

This squawk is produced on a Mac OS X system, and I recall a recent discussion about this being a bug on Xcode or CFE Dev mailing list (but I can't locate it at the moment).

编辑:肖恩·麦克布莱德是一种足以提供一个链接到邮件:的 - fcatch-不确定,行为与readdir的假阳性(+)我不与说法,这可以被忽略同意(因为它从操作系统而不是应用程序来了),特别是因为?它可能会导致 SIGBUS 错误。

Sean McBride was kind enough to provide a link to the email: -fcatch-undefined-behavior false positive with readdir()? I don't agree with the statement that it can be ignored (because its coming from the OS and not the application), especially since it can cause SIGBUS errors.

如何一个重新调整的三分球以清除锵问题( h_addr 是给我的最麻烦的)?可它甚至会由于苹果公司的内部结构,这取决于它做?

How does one re-align the pointers to clear the Clang issues (h_addr is giving me the most trouble)? Can it even be done due to Apple's internal structures depending upon it?

推荐答案

如果你已经有了一个指针数据,有正确的价值观,但在一个不正确的偏移,可能需要的memcpy 数据。在你的情况,把的IPv4 入堆栈变量(将对齐),而的memcpy s.h_addr_list [0] 的IPv4

IF you've got a pointer to data which holds the correct values, but at an incorrect offset, you might need to memcpy that data. In your case, turn ipv4 into a stack variable (which will be aligned), and memcpy the s.h_addr_list[0] to ipv4.

这篇关于如何修复未对齐指针(字符**)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆