File: bugprone-posix-return.cpp

package info (click to toggle)
llvm-toolchain-9 1%3A9.0.1-16.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 882,388 kB
  • sloc: cpp: 4,167,636; ansic: 714,256; asm: 457,610; python: 155,927; objc: 65,094; sh: 42,856; lisp: 26,908; perl: 7,786; pascal: 7,722; makefile: 6,881; ml: 5,581; awk: 3,648; cs: 2,027; xml: 888; javascript: 381; ruby: 156
file content (127 lines) | stat: -rw-r--r-- 4,949 bytes parent folder | download | duplicates (2)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// RUN: %check_clang_tidy %s bugprone-posix-return %t

#define NULL nullptr
#define ZERO 0
#define NEGATIVE_ONE -1

typedef int pid_t;
typedef long off_t;
typedef decltype(sizeof(int)) size_t;
typedef struct __posix_spawn_file_actions* posix_spawn_file_actions_t;
typedef struct __posix_spawnattr* posix_spawnattr_t;

extern "C" int posix_fadvise(int fd, off_t offset, off_t len, int advice);
extern "C" int posix_fallocate(int fd, off_t offset, off_t len);
extern "C" int posix_madvise(void *addr, size_t len, int advice);
extern "C" int posix_memalign(void **memptr, size_t alignment, size_t size);
extern "C" int posix_openpt(int flags);
extern "C" int posix_spawn(pid_t *pid, const char *path,
                const posix_spawn_file_actions_t *file_actions,
                const posix_spawnattr_t *attrp,
                char *const argv[], char *const envp[]);
extern "C" int posix_spawnp(pid_t *pid, const char *file,
                 const posix_spawn_file_actions_t *file_actions,
                 const posix_spawnattr_t *attrp,
                 char *const argv[], char *const envp[]);

void warningLessThanZero() {
  if (posix_fadvise(0, 0, 0, 0) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: the comparison always evaluates to false because posix_fadvise always returns non-negative values
  // CHECK-FIXES: posix_fadvise(0, 0, 0, 0) > 0
  if (posix_fallocate(0, 0, 0) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:32: warning:
  // CHECK-FIXES: posix_fallocate(0, 0, 0) > 0
  if (posix_madvise(NULL, 0, 0) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  // CHECK-FIXES: posix_madvise(NULL, 0, 0) > 0
  if (posix_memalign(NULL, 0, 0) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:34: warning:
  // CHECK-FIXES: posix_memalign(NULL, 0, 0) > 0
  if (posix_spawn(NULL, NULL, NULL, NULL, {NULL}, {NULL}) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:59: warning:
  // CHECK-FIXES: posix_spawn(NULL, NULL, NULL, NULL, {NULL}, {NULL}) > 0
  if (posix_spawnp(NULL, NULL, NULL, NULL, {NULL}, {NULL}) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:60: warning:
  // CHECK-FIXES: posix_spawnp(NULL, NULL, NULL, NULL, {NULL}, {NULL}) > 0
}

void warningAlwaysTrue() {
  if (posix_fadvise(0, 0, 0, 0) >= 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: the comparison always evaluates to true because posix_fadvise always returns non-negative values
}

void warningEqualsNegative() {
  if (posix_fadvise(0, 0, 0, 0) == -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: posix_fadvise
  if (posix_fadvise(0, 0, 0, 0) != -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_fadvise(0, 0, 0, 0) <= -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_fadvise(0, 0, 0, 0) < -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_fallocate(0, 0, 0) == -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:32: warning:
  if (posix_madvise(NULL, 0, 0) == -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_memalign(NULL, 0, 0) == -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:34: warning:
  if (posix_spawn(NULL, NULL, NULL, NULL, {NULL}, {NULL}) == -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:59: warning:
  if (posix_spawnp(NULL, NULL, NULL, NULL, {NULL}, {NULL}) == -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:60: warning:
}

void WarningWithMacro() {
  if (posix_fadvise(0, 0, 0, 0) < ZERO) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  // CHECK-FIXES: posix_fadvise(0, 0, 0, 0) > ZERO
  if (posix_fadvise(0, 0, 0, 0) >= ZERO) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_fadvise(0, 0, 0, 0) == NEGATIVE_ONE) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_fadvise(0, 0, 0, 0) != NEGATIVE_ONE) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_fadvise(0, 0, 0, 0) <= NEGATIVE_ONE) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_fadvise(0, 0, 0, 0) < NEGATIVE_ONE) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
}

void noWarning() {
  if (posix_openpt(0) < 0) {}
  if (posix_openpt(0) <= 0) {}
  if (posix_openpt(0) == -1) {}
  if (posix_openpt(0) != -1) {}
  if (posix_openpt(0) <= -1) {}
  if (posix_openpt(0) < -1) {}
  if (posix_fadvise(0, 0, 0, 0) <= 0) {}
  if (posix_fadvise(0, 0, 0, 0) == 1) {}
}

namespace i {
int posix_fadvise(int fd, off_t offset, off_t len, int advice);

void noWarning() {
  if (posix_fadvise(0, 0, 0, 0) < 0) {}
  if (posix_fadvise(0, 0, 0, 0) >= 0) {}
  if (posix_fadvise(0, 0, 0, 0) == -1) {}
  if (posix_fadvise(0, 0, 0, 0) != -1) {}
  if (posix_fadvise(0, 0, 0, 0) <= -1) {}
  if (posix_fadvise(0, 0, 0, 0) < -1) {}
}

} // namespace i

class G {
 public:
  int posix_fadvise(int fd, off_t offset, off_t len, int advice);

  void noWarning() {
    if (posix_fadvise(0, 0, 0, 0) < 0) {}
    if (posix_fadvise(0, 0, 0, 0) >= 0) {}
    if (posix_fadvise(0, 0, 0, 0) == -1) {}
    if (posix_fadvise(0, 0, 0, 0) != -1) {}
    if (posix_fadvise(0, 0, 0, 0) <= -1) {}
    if (posix_fadvise(0, 0, 0, 0) < -1) {}
  }
};