xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-memsize-comparison.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc 
4*0a6a1f1dSLionel Sambuc typedef __SIZE_TYPE__ size_t;
5*0a6a1f1dSLionel Sambuc extern "C" void *memset(void *, int, size_t);
6*0a6a1f1dSLionel Sambuc extern "C" void *memmove(void *s1, const void *s2, size_t n);
7*0a6a1f1dSLionel Sambuc extern "C" void *memcpy(void *s1, const void *s2, size_t n);
8*0a6a1f1dSLionel Sambuc extern "C" void *memcmp(void *s1, const void *s2, size_t n);
9*0a6a1f1dSLionel Sambuc extern "C" int strncmp(const char *s1, const char *s2, size_t n);
10*0a6a1f1dSLionel Sambuc extern "C" int strncasecmp(const char *s1, const char *s2, size_t n);
11*0a6a1f1dSLionel Sambuc extern "C" char *strncpy(char *dst, const char *src, size_t n);
12*0a6a1f1dSLionel Sambuc extern "C" char *strncat(char *dst, const char *src, size_t n);
13*0a6a1f1dSLionel Sambuc extern "C" char *strndup(const  char *src, size_t n);
14*0a6a1f1dSLionel Sambuc extern "C" size_t strlcpy(char *dst, const char *src, size_t size);
15*0a6a1f1dSLionel Sambuc extern "C" size_t strlcat(char *dst, const char *src, size_t size);
16*0a6a1f1dSLionel Sambuc 
f()17*0a6a1f1dSLionel Sambuc void f() {
18*0a6a1f1dSLionel Sambuc   char b1[80], b2[80];
19*0a6a1f1dSLionel Sambuc   if (memset(b1, 0, sizeof(b1) != 0)) {} // \
20*0a6a1f1dSLionel Sambuc     expected-warning{{size argument in 'memset' call is a comparison}} \
21*0a6a1f1dSLionel Sambuc     expected-note {{did you mean to compare}} \
22*0a6a1f1dSLionel Sambuc     expected-note {{explicitly cast the argument}}
23*0a6a1f1dSLionel Sambuc   if (memset(b1, 0, sizeof(b1)) != 0) {}
24*0a6a1f1dSLionel Sambuc 
25*0a6a1f1dSLionel Sambuc   if (memmove(b1, b2, sizeof(b1) == 0)) {} // \
26*0a6a1f1dSLionel Sambuc     expected-warning{{size argument in 'memmove' call is a comparison}} \
27*0a6a1f1dSLionel Sambuc     expected-note {{did you mean to compare}} \
28*0a6a1f1dSLionel Sambuc     expected-note {{explicitly cast the argument}}
29*0a6a1f1dSLionel Sambuc   if (memmove(b1, b2, sizeof(b1)) == 0) {}
30*0a6a1f1dSLionel Sambuc 
31*0a6a1f1dSLionel Sambuc   if (memcpy(b1, b2, sizeof(b1) < 0)) {} // \
32*0a6a1f1dSLionel Sambuc     expected-warning{{size argument in 'memcpy' call is a comparison}} \
33*0a6a1f1dSLionel Sambuc     expected-note {{did you mean to compare}} \
34*0a6a1f1dSLionel Sambuc     expected-note {{explicitly cast the argument}}
35*0a6a1f1dSLionel Sambuc   if (memcpy(b1, b2, sizeof(b1)) < 0) {}
36*0a6a1f1dSLionel Sambuc 
37*0a6a1f1dSLionel Sambuc   if (memcmp(b1, b2, sizeof(b1) <= 0)) {} // \
38*0a6a1f1dSLionel Sambuc     expected-warning{{size argument in 'memcmp' call is a comparison}} \
39*0a6a1f1dSLionel Sambuc     expected-note {{did you mean to compare}} \
40*0a6a1f1dSLionel Sambuc     expected-note {{explicitly cast the argument}}
41*0a6a1f1dSLionel Sambuc   if (memcmp(b1, b2, sizeof(b1)) <= 0) {}
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc   if (strncmp(b1, b2, sizeof(b1) > 0)) {} // \
44*0a6a1f1dSLionel Sambuc     expected-warning{{size argument in 'strncmp' call is a comparison}} \
45*0a6a1f1dSLionel Sambuc     expected-note {{did you mean to compare}} \
46*0a6a1f1dSLionel Sambuc     expected-note {{explicitly cast the argument}}
47*0a6a1f1dSLionel Sambuc   if (strncmp(b1, b2, sizeof(b1)) > 0) {}
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc   if (strncasecmp(b1, b2, sizeof(b1) >= 0)) {} // \
50*0a6a1f1dSLionel Sambuc     expected-warning{{size argument in 'strncasecmp' call is a comparison}} \
51*0a6a1f1dSLionel Sambuc     expected-note {{did you mean to compare}} \
52*0a6a1f1dSLionel Sambuc     expected-note {{explicitly cast the argument}}
53*0a6a1f1dSLionel Sambuc   if (strncasecmp(b1, b2, sizeof(b1)) >= 0) {}
54*0a6a1f1dSLionel Sambuc 
55*0a6a1f1dSLionel Sambuc   if (strncpy(b1, b2, sizeof(b1) == 0 || true)) {} // \
56*0a6a1f1dSLionel Sambuc     expected-warning{{size argument in 'strncpy' call is a comparison}} \
57*0a6a1f1dSLionel Sambuc     expected-note {{did you mean to compare}} \
58*0a6a1f1dSLionel Sambuc     expected-note {{explicitly cast the argument}}
59*0a6a1f1dSLionel Sambuc   if (strncpy(b1, b2, sizeof(b1)) == 0 || true) {}
60*0a6a1f1dSLionel Sambuc 
61*0a6a1f1dSLionel Sambuc   if (strncat(b1, b2, sizeof(b1) - 1 >= 0 && true)) {} // \
62*0a6a1f1dSLionel Sambuc     expected-warning{{size argument in 'strncat' call is a comparison}} \
63*0a6a1f1dSLionel Sambuc     expected-note {{did you mean to compare}} \
64*0a6a1f1dSLionel Sambuc     expected-note {{explicitly cast the argument}}
65*0a6a1f1dSLionel Sambuc   if (strncat(b1, b2, sizeof(b1) - 1) >= 0 && true) {}
66*0a6a1f1dSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc   if (strndup(b1, sizeof(b1) != 0)) {} // \
68*0a6a1f1dSLionel Sambuc     expected-warning{{size argument in 'strndup' call is a comparison}} \
69*0a6a1f1dSLionel Sambuc     expected-note {{did you mean to compare}} \
70*0a6a1f1dSLionel Sambuc     expected-note {{explicitly cast the argument}}
71*0a6a1f1dSLionel Sambuc   if (strndup(b1, sizeof(b1)) != 0) {}
72*0a6a1f1dSLionel Sambuc 
73*0a6a1f1dSLionel Sambuc   if (strlcpy(b1, b2, sizeof(b1) != 0)) {} // \
74*0a6a1f1dSLionel Sambuc     expected-warning{{size argument in 'strlcpy' call is a comparison}} \
75*0a6a1f1dSLionel Sambuc     expected-note {{did you mean to compare}} \
76*0a6a1f1dSLionel Sambuc     expected-note {{explicitly cast the argument}}
77*0a6a1f1dSLionel Sambuc   if (strlcpy(b1, b2, sizeof(b1)) != 0) {}
78*0a6a1f1dSLionel Sambuc 
79*0a6a1f1dSLionel Sambuc   if (strlcat(b1, b2, sizeof(b1) != 0)) {} // \
80*0a6a1f1dSLionel Sambuc     expected-warning{{size argument in 'strlcat' call is a comparison}} \
81*0a6a1f1dSLionel Sambuc     expected-note {{did you mean to compare}} \
82*0a6a1f1dSLionel Sambuc     expected-note {{explicitly cast the argument}}
83*0a6a1f1dSLionel Sambuc   if (strlcat(b1, b2, sizeof(b1)) != 0) {}
84*0a6a1f1dSLionel Sambuc 
85*0a6a1f1dSLionel Sambuc   if (memset(b1, 0, sizeof(b1) / 2)) {}
86*0a6a1f1dSLionel Sambuc   if (memset(b1, 0, sizeof(b1) >> 2)) {}
87*0a6a1f1dSLionel Sambuc   if (memset(b1, 0, 4 << 2)) {}
88*0a6a1f1dSLionel Sambuc   if (memset(b1, 0, 4 + 2)) {}
89*0a6a1f1dSLionel Sambuc   if (memset(b1, 0, 4 - 2)) {}
90*0a6a1f1dSLionel Sambuc   if (memset(b1, 0, 4 * 2)) {}
91*0a6a1f1dSLionel Sambuc 
92*0a6a1f1dSLionel Sambuc   if (memset(b1, 0, (size_t)(sizeof(b1) != 0))) {}
93*0a6a1f1dSLionel Sambuc }
94