[PATCH] Warn on mismatching types to sizeof for memset and friends where length is of the form sizeof(Type) * factor.

Oliver Chang ochang at google.com
Wed Apr 8 17:09:06 PDT 2015


Here are some general results after some quick testing (some of which is stolen from thakis's testing on chromium). Unfortunately some of these warnings were difficult to triage and requires a significant amount of time (which I think makes a point for this warning).

Discounting duplicates in the same file, both Firefox and Chromium give around 8-10 warnings.

Some patterns observed:

- Most seem to just be based on size assumptions between types, e.g. 2 * sizeof(uint16_t) == sizeof(uint32_t).

Firefox had about 2 instances where 2 different classes were expected to have the same size (e.g. when 1 is a wrapper around another).

- In Chromium there are some issues with structs and unions that solely contained members of a single type.

e.g.
struct M {

  int A[4];

};

M a;
memset(&a, 0, sizeof(int) * 4);

thakis@ suggested decaying the struct type to the member type in this case for the comparison.

- Firefox also had about 2 warnings related to multidimensional arrays, e.g.

typedef int Foo[10];
Foo m;
memset(&m, 0, sizeof(Foo));

Perhaps we can just compare the type 'Foo' in this case before trying to break it down further in this case.

- There are some other warnings such as differences complex_t vs float[2], w_char vs short.


http://reviews.llvm.org/D8881

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/






More information about the cfe-commits mailing list