Clang Project

clang_source_code/test/SemaCXX/warn-memsize-comparison.cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2//
3
4typedef __SIZE_TYPE__ size_t;
5extern "C" void *memset(void *, int, size_t);
6extern "C" void *memmove(void *s1, const void *s2, size_t n);
7extern "C" void *memcpy(void *s1, const void *s2, size_t n);
8extern "C" int memcmp(void *s1, const void *s2, size_t n);
9extern "C" int strncmp(const char *s1, const char *s2, size_t n);
10extern "C" int strncasecmp(const char *s1, const char *s2, size_t n);
11extern "C" char *strncpy(char *dst, const char *src, size_t n);
12extern "C" char *strncat(char *dst, const char *src, size_t n);
13extern "C" char *strndup(const  char *src, size_t n);
14extern "C" size_t strlcpy(char *dst, const char *src, size_t size);
15extern "C" size_t strlcat(char *dst, const char *src, size_t size);
16
17void 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  // 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}}
37
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) {}
43
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) {}
49
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) {}
55
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) {}
61
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}}
68
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) {}
74
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) {}
80
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) {}
86
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)) {}
93
94  if (memset(b1, 0, (size_t)(sizeof(b1) != 0))) {}
95}
96