[prev in list] [next in list] [prev in thread] [next in thread] 

List:       llvm-bugs
Subject:    [llvm-bugs] [Bug 39113] New: -Waddress-of-packed-member false positive on sizeof(*&struct->field)
From:       via llvm-bugs <llvm-bugs () lists ! llvm ! org>
Date:       2018-09-28 13:11:03
Message-ID: bug-39113-206 () http ! bugs ! llvm ! org/
[Download RAW message or body]

--1538140263.55e7A0.2281
Date: Fri, 28 Sep 2018 06:11:03 -0700
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Bugzilla-URL: http://bugs.llvm.org/
Auto-Submitted: auto-generated

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@nondot.org
          Reporter: peter.maydell@linaro.org
                CC: llvm-bugs@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.
--1538140263.55e7A0.2281
Date: Fri, 28 Sep 2018 06:11:03 -0700
MIME-Version: 1.0
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Bugzilla-URL: http://bugs.llvm.org/
Auto-Submitted: auto-generated

<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - -Waddress-of-packed-member false positive on \
sizeof(*&amp;struct-&gt;field)"  \
href="https://bugs.llvm.org/show_bug.cgi?id=39113">39113</a>  </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>-Waddress-of-packed-member false positive on \
sizeof(*&amp;struct-&gt;field)  </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>7.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs&#64;nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>peter.maydell&#64;linaro.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs&#64;lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>clang's -Waddress-of-packed-member warning gives false positives on \
constructs like &quot;sizeof(*&amp;px-&gt;i)&quot;. 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(*&amp;px-&gt;i) v = 0;
     return v + sizeof(*&amp;px-&gt;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(*&amp;px-&gt;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(*&amp;px-&gt;i);
                         ^~~~~
2 warnings generated.

Tested using clang-7 from the Ubuntu Xenial packages at <a \
href="http://apt.llvm.org/">http://apt.llvm.org/</a>.

(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 \
&quot;*&amp;&quot; construction; the * is in the macro and the &amp; in its caller.
<a href="https://git.qemu.org/?p=qemu.git;a=blob;f=linux-user/qemu.h;h=b4959e41c6e384f \
4523573049db4b56a22ee3880;hb=38441756b70eec5807b5f60dad11a93a91199866#l471">https://gi \
t.qemu.org/?p=qemu.git;a=blob;f=linux-user/qemu.h;h=b4959e41c6e384f4523573049db4b56a22ee3880;hb=38441756b70eec5807b5f60dad11a93a91199866#l471</a>


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 &quot;__get_user_e(x,
&amp;packed_struct-&gt;member)&quot;.)</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>
--1538140263.55e7A0.2281--


[Attachment #3 (text/plain)]

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic