You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Sema] Add a new warning, -Wmemset-transposed-args
This diagnoses calls to memset that have the second and third arguments
transposed, for example:
memset(buf, sizeof(buf), 0);
This is done by checking if the third argument is a literal 0, or if the second
is a sizeof expression (and the third isn't). The first check is also done for
calls to bzero.
Differential revision: https://reviews.llvm.org/D49112
llvm-svn: 337470
memset(array, sizeof(array), 0); // expected-warning{{'size' argument to memset is '0'; did you mean to transpose the last two arguments?}} expected-note{{parenthesize the third argument to silence}}
13
+
memset(array, sizeof(array), 0xff); // expected-warning{{setting buffer to a 'sizeof' expression; did you mean to transpose the last two arguments?}} expected-note{{cast the second argument to 'int' to silence}}
14
+
memset(ptr, sizeof(ptr), 0); // expected-warning{{'size' argument to memset is '0'; did you mean to transpose the last two arguments?}} expected-note{{parenthesize the third argument to silence}}
15
+
memset(ptr, sizeof(*ptr) *10, 1); // expected-warning{{setting buffer to a 'sizeof' expression; did you mean to transpose the last two arguments?}} expected-note{{cast the second argument to 'int' to silence}}
16
+
memset(ptr, 10*sizeof(int*), 1); // expected-warning{{setting buffer to a 'sizeof' expression; did you mean to transpose the last two arguments?}} expected-note{{cast the second argument to 'int' to silence}}
17
+
memset(ptr, 10*sizeof(int*) +10, 0xff); // expected-warning{{setting buffer to a 'sizeof' expression; did you mean to transpose the last two arguments?}} expected-note{{cast the second argument to 'int' to silence}}
18
+
memset(ptr, sizeof(char) *sizeof(int*), 0xff); // expected-warning{{setting buffer to a 'sizeof' expression; did you mean to transpose the last two arguments?}} expected-note{{cast the second argument to 'int' to silence}}
19
+
memset(array, sizeof(array), sizeof(array)); // Uh... fine I guess.
20
+
memset(array, 0, sizeof(array));
21
+
memset(ptr, 0, sizeof(int*) *10);
22
+
memset(array, (int)sizeof(array), (0)); // no warning
23
+
memset(array, (int)sizeof(array), 32); // no warning
24
+
memset(array, 32, (0)); // no warning
25
+
26
+
bzero(ptr, 0); // expected-warning{{'size' argument to bzero is '0'}} expected-note{{parenthesize the second argument to silence}}
27
+
real_bzero(ptr, 0); // expected-warning{{'size' argument to bzero is '0'}} expected-note{{parenthesize the second argument to silence}}
28
+
}
29
+
30
+
voidmacros() {
31
+
#defineZERO 0
32
+
intarray[10];
33
+
memset(array, 0xff, ZERO); // no warning
34
+
// Still emit a diagnostic for memsetting a sizeof expression:
0 commit comments