• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 //
3 
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" void *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);
16 
f()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) {}
24 
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) {}
30 
31   if (memcpy(b1, b2, sizeof(b1) < 0)) {} // \
32     expected-warning{{size argument in 'memcpy' call is a comparison}} \
33     expected-note {{did you mean to compare}} \
34     expected-note {{explicitly cast the argument}}
35   if (memcpy(b1, b2, sizeof(b1)) < 0) {}
36 
37   if (memcmp(b1, b2, sizeof(b1) <= 0)) {} // \
38     expected-warning{{size argument in 'memcmp' call is a comparison}} \
39     expected-note {{did you mean to compare}} \
40     expected-note {{explicitly cast the argument}}
41   if (memcmp(b1, b2, sizeof(b1)) <= 0) {}
42 
43   if (strncmp(b1, b2, sizeof(b1) > 0)) {} // \
44     expected-warning{{size argument in 'strncmp' call is a comparison}} \
45     expected-note {{did you mean to compare}} \
46     expected-note {{explicitly cast the argument}}
47   if (strncmp(b1, b2, sizeof(b1)) > 0) {}
48 
49   if (strncasecmp(b1, b2, sizeof(b1) >= 0)) {} // \
50     expected-warning{{size argument in 'strncasecmp' call is a comparison}} \
51     expected-note {{did you mean to compare}} \
52     expected-note {{explicitly cast the argument}}
53   if (strncasecmp(b1, b2, sizeof(b1)) >= 0) {}
54 
55   if (strncpy(b1, b2, sizeof(b1) == 0 || true)) {} // \
56     expected-warning{{size argument in 'strncpy' call is a comparison}} \
57     expected-note {{did you mean to compare}} \
58     expected-note {{explicitly cast the argument}}
59   if (strncpy(b1, b2, sizeof(b1)) == 0 || true) {}
60 
61   if (strncat(b1, b2, sizeof(b1) - 1 >= 0 && true)) {} // \
62     expected-warning{{size argument in 'strncat' call is a comparison}} \
63     expected-note {{did you mean to compare}} \
64     expected-note {{explicitly cast the argument}}
65   if (strncat(b1, b2, sizeof(b1) - 1) >= 0 && true) {}
66 
67   if (strndup(b1, sizeof(b1) != 0)) {} // \
68     expected-warning{{size argument in 'strndup' call is a comparison}} \
69     expected-note {{did you mean to compare}} \
70     expected-note {{explicitly cast the argument}}
71   if (strndup(b1, sizeof(b1)) != 0) {}
72 
73   if (strlcpy(b1, b2, sizeof(b1) != 0)) {} // \
74     expected-warning{{size argument in 'strlcpy' call is a comparison}} \
75     expected-note {{did you mean to compare}} \
76     expected-note {{explicitly cast the argument}}
77   if (strlcpy(b1, b2, sizeof(b1)) != 0) {}
78 
79   if (strlcat(b1, b2, sizeof(b1) != 0)) {} // \
80     expected-warning{{size argument in 'strlcat' call is a comparison}} \
81     expected-note {{did you mean to compare}} \
82     expected-note {{explicitly cast the argument}}
83   if (strlcat(b1, b2, sizeof(b1)) != 0) {}
84 
85   if (memset(b1, 0, sizeof(b1) / 2)) {}
86   if (memset(b1, 0, sizeof(b1) >> 2)) {}
87   if (memset(b1, 0, 4 << 2)) {}
88   if (memset(b1, 0, 4 + 2)) {}
89   if (memset(b1, 0, 4 - 2)) {}
90   if (memset(b1, 0, 4 * 2)) {}
91 
92   if (memset(b1, 0, (size_t)(sizeof(b1) != 0))) {}
93 }
94