[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / warn-memsize-comparison.cpp
blobb5c7a9d6969cc26d23e94c956598a620e0e4b3c3
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 //
4 typedef __SIZE_TYPE__ size_t;
5 extern "C" void *memset(void *, int, size_t);
6 extern "C" void *memmove(void *s1, const void *s2, size_t n);
7 extern "C" void *memcpy(void *s1, const void *s2, size_t n);
8 extern "C" int memcmp(void *s1, const void *s2, size_t n);
9 extern "C" int strncmp(const char *s1, const char *s2, size_t n);
10 extern "C" int strncasecmp(const char *s1, const char *s2, size_t n);
11 extern "C" char *strncpy(char *dst, const char *src, size_t n);
12 extern "C" char *strncat(char *dst, const char *src, size_t n);
13 extern "C" char *strndup(const char *src, size_t n);
14 extern "C" size_t strlcpy(char *dst, const char *src, size_t size);
15 extern "C" size_t strlcat(char *dst, const char *src, size_t size);
17 void f() {
18 char b1[80], b2[80];
19 if (memset(b1, 0, sizeof(b1) != 0)) {} // \
20 expected-warning{{size argument in 'memset' call is a comparison}} \
21 expected-note {{did you mean to compare}} \
22 expected-note {{explicitly cast the argument}}
23 if (memset(b1, 0, sizeof(b1)) != 0) {}
25 if (memmove(b1, b2, sizeof(b1) == 0)) {} // \
26 expected-warning{{size argument in 'memmove' call is a comparison}} \
27 expected-note {{did you mean to compare}} \
28 expected-note {{explicitly cast the argument}}
29 if (memmove(b1, b2, sizeof(b1)) == 0) {}
31 // FIXME: This fixit is bogus.
32 if (memcpy(b1, b2, sizeof(b1) < 0)) {} // \
33 expected-warning{{size argument in 'memcpy' call is a comparison}} \
34 expected-note {{did you mean to compare}} \
35 expected-note {{explicitly cast the argument}}
36 if (memcpy(b1, b2, sizeof(b1)) < 0) {} // expected-error {{ordered comparison between pointer and zero}}
38 if (memcmp(b1, b2, sizeof(b1) <= 0)) {} // \
39 expected-warning{{size argument in 'memcmp' call is a comparison}} \
40 expected-note {{did you mean to compare}} \
41 expected-note {{explicitly cast the argument}}
42 if (memcmp(b1, b2, sizeof(b1)) <= 0) {}
44 if (strncmp(b1, b2, sizeof(b1) > 0)) {} // \
45 expected-warning{{size argument in 'strncmp' call is a comparison}} \
46 expected-note {{did you mean to compare}} \
47 expected-note {{explicitly cast the argument}}
48 if (strncmp(b1, b2, sizeof(b1)) > 0) {}
50 if (strncasecmp(b1, b2, sizeof(b1) >= 0)) {} // \
51 expected-warning{{size argument in 'strncasecmp' call is a comparison}} \
52 expected-note {{did you mean to compare}} \
53 expected-note {{explicitly cast the argument}}
54 if (strncasecmp(b1, b2, sizeof(b1)) >= 0) {}
56 if (strncpy(b1, b2, sizeof(b1) == 0 || true)) {} // \
57 expected-warning{{size argument in 'strncpy' call is a comparison}} \
58 expected-note {{did you mean to compare}} \
59 expected-note {{explicitly cast the argument}}
60 if (strncpy(b1, b2, sizeof(b1)) == 0 || true) {}
62 // FIXME: This fixit is bogus.
63 if (strncat(b1, b2, sizeof(b1) - 1 >= 0 && true)) {} // \
64 expected-warning{{size argument in 'strncat' call is a comparison}} \
65 expected-note {{did you mean to compare}} \
66 expected-note {{explicitly cast the argument}}
67 if (strncat(b1, b2, sizeof(b1) - 1) >= 0 && true) {} // expected-error {{ordered comparison between pointer and zero}}
69 if (strndup(b1, sizeof(b1) != 0)) {} // \
70 expected-warning{{size argument in 'strndup' call is a comparison}} \
71 expected-note {{did you mean to compare}} \
72 expected-note {{explicitly cast the argument}}
73 if (strndup(b1, sizeof(b1)) != 0) {}
75 if (strlcpy(b1, b2, sizeof(b1) != 0)) {} // \
76 expected-warning{{size argument in 'strlcpy' call is a comparison}} \
77 expected-note {{did you mean to compare}} \
78 expected-note {{explicitly cast the argument}}
79 if (strlcpy(b1, b2, sizeof(b1)) != 0) {}
81 if (strlcat(b1, b2, sizeof(b1) != 0)) {} // \
82 expected-warning{{size argument in 'strlcat' call is a comparison}} \
83 expected-note {{did you mean to compare}} \
84 expected-note {{explicitly cast the argument}}
85 if (strlcat(b1, b2, sizeof(b1)) != 0) {}
87 if (memset(b1, 0, sizeof(b1) / 2)) {}
88 if (memset(b1, 0, sizeof(b1) >> 2)) {}
89 if (memset(b1, 0, 4 << 2)) {}
90 if (memset(b1, 0, 4 + 2)) {}
91 if (memset(b1, 0, 4 - 2)) {}
92 if (memset(b1, 0, 4 * 2)) {}
94 if (memset(b1, 0, (size_t)(sizeof(b1) != 0))) {}