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

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 14 11:50:50 PDT 2016


rsmith added a comment.

This still looks like it will have has lots of false positives for cases like:

  struct __attribute__((packed)) A {
    char c;
    int n;
  } a;
  void *p = &a.n;

It also looks like it will now have false negatives for cases like:

  memcpy(x, y, *&a.n);

I think whitelisting specific functions is not a reasonable approach here; instead, how about deferring the check until you see how the misaligned pointer is used? A couple of approaches seem feasible:

- you could extend the conversion-checking code in SemaChecking to look for such misaligned operations that are not immediately converted to a pointer type with suitable (lower) alignment requirements
- you could build a list in Sema of the cases that are pending a diagnostic, produce diagnostics at the end of the full-expression, and remove items from the list when you see a suitable conversion applied to them

In any case, this diagnostic should apply to reference binding as well as pointers.

GCC appears to check this as a separate step, at least after it applies its fold; for example:

  struct __attribute__((packed, aligned(4))) S { char c[4]; int n; } s;
  int k;
  int &r = true ? s.n : k; // gcc rejects, "cannot bind paced field 's.S::n' to 'int&'
  int &s = false ? s.n : k; // gcc accepts


http://reviews.llvm.org/D20561





More information about the cfe-commits mailing list