[PATCH] D20561: Warn when taking address of packed member

Roger Ferrer Ibanez via cfe-commits cfe-commits at lists.llvm.org
Thu May 26 08:15:23 PDT 2016


rogfer01 marked 4 inline comments as done.

================
Comment at: lib/Sema/SemaExpr.cpp:10527
@@ +10526,3 @@
+        ME->getMemberDecl()->hasAttr<PackedAttr>()) {
+      Diag(OpLoc, diag::warn_taking_address_of_packed_member)
+          << ME->getMemberDecl() << RD;
----------------
aaron.ballman wrote:
> I wonder if we should diagnose only when the resulting pointer is actually misaligned. For instance, the following code should be just fine:
> ```
> struct __attribute__((packed)) S {
>   int i;
> };
> 
> void f(S s) {
>   int *ip = &s.i; // Why should this warn?
> }
> ```
> Have you run this patch over any large code bases to see how high the false-positive rate is? How do you envision users silencing this diagnostic if they have a false-positive? Something like `&(s.x)` (since I you're not ignoring paren imp casts)?
I think it should warn because in this case &s == &s.i and the alignment of the whole struct is 1 (due to the packed attribute) so s.i is also aligned to 1 which would not be the suitable alignment for an int.

I'm currently building Firefox, I'll add a comment once it ends.

Regarding silencing the diagnostic: -Wno-address-of-packed-member should do but I understand it might be too coarse in some cases.


http://reviews.llvm.org/D20561





More information about the cfe-commits mailing list