[llvm-bugs] [Bug 39113] New: -Waddress-of-packed-member false positive on sizeof(*&struct->field)

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Sep 28 06:11:03 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=39113

            Bug ID: 39113
           Summary: -Waddress-of-packed-member false positive on
                    sizeof(*&struct->field)
           Product: clang
           Version: 7.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: peter.maydell at linaro.org
                CC: llvm-bugs at lists.llvm.org

clang's -Waddress-of-packed-member warning gives false positives on constructs
like "sizeof(*&px->i)". This doesn't actually ever dereference the member
field,  so there is never an unaligned pointer in play, and it shouldn't cause
a warning. The same thing applies to typeof() and possibly other similar
constructs.

Reproducer:

$ cat zz9.c
struct x {
    char c;
    int i;
} __attribute__((packed));

int main(void) {
     struct x *px;
     typeof(*&px->i) v = 0;
     return v + sizeof(*&px->i);
}
$ clang-7 -g -Wall -o zz9.o -c zz9.c
zz9.c:8:15: warning: taking address of packed member 'i' of class or
      structure 'x' may result in an unaligned pointer value
      [-Waddress-of-packed-member]
     typeof(*&px->i) v = 0;
              ^~~~~
zz9.c:9:26: warning: taking address of packed member 'i' of class or
      structure 'x' may result in an unaligned pointer value
      [-Waddress-of-packed-member]
     return v + sizeof(*&px->i);
                         ^~~~~
2 warnings generated.

Tested using clang-7 from the Ubuntu Xenial packages at http://apt.llvm.org/.

(The real world code that triggers this false-positive is in QEMU. It's in a
macro which takes an address, which is the cause of the odd-looking "*&"
construction; the * is in the macro and the & in its caller.
https://git.qemu.org/?p=qemu.git;a=blob;f=linux-user/qemu.h;h=b4959e41c6e384f4523573049db4b56a22ee3880;hb=38441756b70eec5807b5f60dad11a93a91199866#l471

The __get_user_e() and __put_user_e() macros trigger the warning for the sizeof
and typeof uses if the user of the macro does "__get_user_e(x,
&packed_struct->member)".)

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180928/331e8d09/attachment-0001.html>


More information about the llvm-bugs mailing list