File: warn-memsize-comparison.cpp

package info (click to toggle)
llvm-toolchain-6.0 1%3A6.0.1-10
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 598,080 kB
  • sloc: cpp: 3,046,253; ansic: 595,057; asm: 271,965; python: 128,926; objc: 106,554; sh: 21,906; lisp: 10,191; pascal: 6,094; ml: 5,544; perl: 5,265; makefile: 2,227; cs: 2,027; xml: 686; php: 212; csh: 117
file content (95 lines) | stat: -rw-r--r-- 4,197 bytes parent folder | download | duplicates (29)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// RUN: %clang_cc1 -fsyntax-only -verify %s
//

typedef __SIZE_TYPE__ size_t;
extern "C" void *memset(void *, int, size_t);
extern "C" void *memmove(void *s1, const void *s2, size_t n);
extern "C" void *memcpy(void *s1, const void *s2, size_t n);
extern "C" int memcmp(void *s1, const void *s2, size_t n);
extern "C" int strncmp(const char *s1, const char *s2, size_t n);
extern "C" int strncasecmp(const char *s1, const char *s2, size_t n);
extern "C" char *strncpy(char *dst, const char *src, size_t n);
extern "C" char *strncat(char *dst, const char *src, size_t n);
extern "C" char *strndup(const  char *src, size_t n);
extern "C" size_t strlcpy(char *dst, const char *src, size_t size);
extern "C" size_t strlcat(char *dst, const char *src, size_t size);

void f() {
  char b1[80], b2[80];
  if (memset(b1, 0, sizeof(b1) != 0)) {} // \
    expected-warning{{size argument in 'memset' call is a comparison}} \
    expected-note {{did you mean to compare}} \
    expected-note {{explicitly cast the argument}}
  if (memset(b1, 0, sizeof(b1)) != 0) {}

  if (memmove(b1, b2, sizeof(b1) == 0)) {} // \
    expected-warning{{size argument in 'memmove' call is a comparison}} \
    expected-note {{did you mean to compare}} \
    expected-note {{explicitly cast the argument}}
  if (memmove(b1, b2, sizeof(b1)) == 0) {}

  // FIXME: This fixit is bogus.
  if (memcpy(b1, b2, sizeof(b1) < 0)) {} // \
    expected-warning{{size argument in 'memcpy' call is a comparison}} \
    expected-note {{did you mean to compare}} \
    expected-note {{explicitly cast the argument}}
  if (memcpy(b1, b2, sizeof(b1)) < 0) {} // expected-error {{ordered comparison between pointer and zero}}

  if (memcmp(b1, b2, sizeof(b1) <= 0)) {} // \
    expected-warning{{size argument in 'memcmp' call is a comparison}} \
    expected-note {{did you mean to compare}} \
    expected-note {{explicitly cast the argument}}
  if (memcmp(b1, b2, sizeof(b1)) <= 0) {}

  if (strncmp(b1, b2, sizeof(b1) > 0)) {} // \
    expected-warning{{size argument in 'strncmp' call is a comparison}} \
    expected-note {{did you mean to compare}} \
    expected-note {{explicitly cast the argument}}
  if (strncmp(b1, b2, sizeof(b1)) > 0) {}

  if (strncasecmp(b1, b2, sizeof(b1) >= 0)) {} // \
    expected-warning{{size argument in 'strncasecmp' call is a comparison}} \
    expected-note {{did you mean to compare}} \
    expected-note {{explicitly cast the argument}}
  if (strncasecmp(b1, b2, sizeof(b1)) >= 0) {}

  if (strncpy(b1, b2, sizeof(b1) == 0 || true)) {} // \
    expected-warning{{size argument in 'strncpy' call is a comparison}} \
    expected-note {{did you mean to compare}} \
    expected-note {{explicitly cast the argument}}
  if (strncpy(b1, b2, sizeof(b1)) == 0 || true) {}

  // FIXME: This fixit is bogus.
  if (strncat(b1, b2, sizeof(b1) - 1 >= 0 && true)) {} // \
    expected-warning{{size argument in 'strncat' call is a comparison}} \
    expected-note {{did you mean to compare}} \
    expected-note {{explicitly cast the argument}}
  if (strncat(b1, b2, sizeof(b1) - 1) >= 0 && true) {} // expected-error {{ordered comparison between pointer and zero}}

  if (strndup(b1, sizeof(b1) != 0)) {} // \
    expected-warning{{size argument in 'strndup' call is a comparison}} \
    expected-note {{did you mean to compare}} \
    expected-note {{explicitly cast the argument}}
  if (strndup(b1, sizeof(b1)) != 0) {}

  if (strlcpy(b1, b2, sizeof(b1) != 0)) {} // \
    expected-warning{{size argument in 'strlcpy' call is a comparison}} \
    expected-note {{did you mean to compare}} \
    expected-note {{explicitly cast the argument}}
  if (strlcpy(b1, b2, sizeof(b1)) != 0) {}

  if (strlcat(b1, b2, sizeof(b1) != 0)) {} // \
    expected-warning{{size argument in 'strlcat' call is a comparison}} \
    expected-note {{did you mean to compare}} \
    expected-note {{explicitly cast the argument}}
  if (strlcat(b1, b2, sizeof(b1)) != 0) {}

  if (memset(b1, 0, sizeof(b1) / 2)) {}
  if (memset(b1, 0, sizeof(b1) >> 2)) {}
  if (memset(b1, 0, 4 << 2)) {}
  if (memset(b1, 0, 4 + 2)) {}
  if (memset(b1, 0, 4 - 2)) {}
  if (memset(b1, 0, 4 * 2)) {}

  if (memset(b1, 0, (size_t)(sizeof(b1) != 0))) {}
}