1// RUN: %check_clang_tidy %s bugprone-posix-return %t
2
3#define NULL nullptr
4#define ZERO 0
5#define NEGATIVE_ONE -1
6
7typedef int pid_t;
8typedef long off_t;
9typedef decltype(sizeof(int)) size_t;
10typedef struct __posix_spawn_file_actions* posix_spawn_file_actions_t;
11typedef struct __posix_spawnattr* posix_spawnattr_t;
12# define __CPU_SETSIZE 1024
13# define __NCPUBITS (8 * sizeof (__cpu_mask))
14typedef unsigned long int __cpu_mask;
15typedef struct
16{
17 __cpu_mask __bits[__CPU_SETSIZE / __NCPUBITS];
18} cpu_set_t;
19typedef struct _opaque_pthread_t *__darwin_pthread_t;
20typedef __darwin_pthread_t pthread_t;
21typedef struct pthread_attr_t_ *pthread_attr_t;
22
23extern "C" int posix_fadvise(int fd, off_t offset, off_t len, int advice);
24extern "C" int posix_fallocate(int fd, off_t offset, off_t len);
25extern "C" int posix_madvise(void *addr, size_t len, int advice);
26extern "C" int posix_memalign(void **memptr, size_t alignment, size_t size);
27extern "C" int posix_openpt(int flags);
28extern "C" int posix_spawn(pid_t *pid, const char *path,
29 const posix_spawn_file_actions_t *file_actions,
30 const posix_spawnattr_t *attrp,
31 char *const argv[], char *const envp[]);
32extern "C" int posix_spawnp(pid_t *pid, const char *file,
33 const posix_spawn_file_actions_t *file_actions,
34 const posix_spawnattr_t *attrp,
35 char *const argv[], char *const envp[]);
36extern "C" int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
37extern "C" int pthread_attr_setaffinity_np(pthread_attr_t *attr, size_t cpusetsize, const cpu_set_t *cpuset);
38extern "C" int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
39extern "C" int pthread_attr_init(pthread_attr_t *attr);
40extern "C" int pthread_yield(void);
41
42
43void warningLessThanZero() {
44 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) < 0) {}
45 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: the comparison always evaluates to false because posix_fadvise always returns non-negative values
46 // CHECK-FIXES: posix_fadvise(0, 0, 0, 0) > 0
47 if (posix_fallocate(fd: 0, offset: 0, len: 0) < 0) {}
48 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning:
49 // CHECK-FIXES: posix_fallocate(0, 0, 0) > 0
50 if (posix_madvise(NULL, len: 0, advice: 0) < 0) {}
51 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
52 // CHECK-FIXES: posix_madvise(NULL, 0, 0) > 0
53 if (posix_memalign(NULL, alignment: 0, size: 0) < 0) {}
54 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning:
55 // CHECK-FIXES: posix_memalign(NULL, 0, 0) > 0
56 if (posix_spawn(NULL, NULL, NULL, NULL, argv: {NULL}, envp: {NULL}) < 0) {}
57 // CHECK-MESSAGES: :[[@LINE-1]]:59: warning:
58 // CHECK-FIXES: posix_spawn(NULL, NULL, NULL, NULL, {NULL}, {NULL}) > 0
59 if (posix_spawnp(NULL, NULL, NULL, NULL, argv: {NULL}, envp: {NULL}) < 0) {}
60 // CHECK-MESSAGES: :[[@LINE-1]]:60: warning:
61 // CHECK-FIXES: posix_spawnp(NULL, NULL, NULL, NULL, {NULL}, {NULL}) > 0
62 if (pthread_create(NULL, NULL, NULL, NULL) < 0) {}
63 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: the comparison always evaluates to false because pthread_create always returns non-negative values
64 // CHECK-FIXES: pthread_create(NULL, NULL, NULL, NULL) > 0
65 if (pthread_attr_setaffinity_np(NULL, cpusetsize: 0, NULL) < 0) {}
66 // CHECK-MESSAGES: :[[@LINE-1]]:50: warning:
67 // CHECK-FIXES: pthread_attr_setaffinity_np(NULL, 0, NULL) > 0
68 if (pthread_attr_setschedpolicy(NULL, policy: 0) < 0) {}
69 // CHECK-MESSAGES: :[[@LINE-1]]:44: warning:
70 // CHECK-FIXES: pthread_attr_setschedpolicy(NULL, 0) > 0)
71 if (pthread_attr_init(NULL) < 0) {}
72 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning:
73 // CHECK-FIXES: pthread_attr_init(NULL) > 0
74 if (pthread_yield() < 0) {}
75 // CHECK-MESSAGES: :[[@LINE-1]]:23: warning:
76 // CHECK-FIXES: pthread_yield() > 0
77
78}
79
80void warningAlwaysTrue() {
81 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) >= 0) {}
82 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: the comparison always evaluates to true because posix_fadvise always returns non-negative values
83 if (pthread_create(NULL, NULL, NULL, NULL) >= 0) {}
84 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: the comparison always evaluates to true because pthread_create always returns non-negative values
85 if (pthread_attr_setaffinity_np(NULL, cpusetsize: 0, NULL) >= 0) {}
86 // CHECK-MESSAGES: :[[@LINE-1]]:50: warning:
87 if (pthread_attr_setschedpolicy(NULL, policy: 0) >= 0) {}
88 // CHECK-MESSAGES: :[[@LINE-1]]:44: warning:
89 if (pthread_attr_init(NULL) >= 0) {}
90 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning:
91 if (pthread_yield() >= 0) {}
92 // CHECK-MESSAGES: :[[@LINE-1]]:23: warning:
93
94}
95
96void warningEqualsNegative() {
97 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) == -1) {}
98 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: posix_fadvise
99 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) != -1) {}
100 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
101 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) <= -1) {}
102 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
103 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) < -1) {}
104 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
105 if (posix_fallocate(fd: 0, offset: 0, len: 0) == -1) {}
106 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning:
107 if (posix_madvise(NULL, len: 0, advice: 0) == -1) {}
108 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
109 if (posix_memalign(NULL, alignment: 0, size: 0) == -1) {}
110 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning:
111 if (posix_spawn(NULL, NULL, NULL, NULL, argv: {NULL}, envp: {NULL}) == -1) {}
112 // CHECK-MESSAGES: :[[@LINE-1]]:59: warning:
113 if (posix_spawnp(NULL, NULL, NULL, NULL, argv: {NULL}, envp: {NULL}) == -1) {}
114 // CHECK-MESSAGES: :[[@LINE-1]]:60: warning:
115 if (pthread_create(NULL, NULL, NULL, NULL) == -1) {}
116 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: pthread_create
117 if (pthread_create(NULL, NULL, NULL, NULL) != -1) {}
118 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
119 if (pthread_create(NULL, NULL, NULL, NULL) <= -1) {}
120 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
121 if (pthread_create(NULL, NULL, NULL, NULL) < -1) {}
122 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
123
124}
125
126void WarningWithMacro() {
127 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) < ZERO) {}
128 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
129 // CHECK-FIXES: posix_fadvise(0, 0, 0, 0) > ZERO
130 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) >= ZERO) {}
131 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
132 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) == NEGATIVE_ONE) {}
133 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
134 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) != NEGATIVE_ONE) {}
135 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
136 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) <= NEGATIVE_ONE) {}
137 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
138 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) < NEGATIVE_ONE) {}
139 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
140 if (pthread_create(NULL, NULL, NULL, NULL) < ZERO) {}
141 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
142 // CHECK-FIXES: pthread_create(NULL, NULL, NULL, NULL) > ZERO
143 if (pthread_create(NULL, NULL, NULL, NULL) >= ZERO) {}
144 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
145 if (pthread_create(NULL, NULL, NULL, NULL) == NEGATIVE_ONE) {}
146 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
147 if (pthread_create(NULL, NULL, NULL, NULL) != NEGATIVE_ONE) {}
148 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
149 if (pthread_create(NULL, NULL, NULL, NULL) <= NEGATIVE_ONE) {}
150 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
151 if (pthread_create(NULL, NULL, NULL, NULL) < NEGATIVE_ONE) {}
152 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
153
154}
155
156void noWarning() {
157 if (posix_openpt(flags: 0) < 0) {}
158 if (posix_openpt(flags: 0) <= 0) {}
159 if (posix_openpt(flags: 0) == -1) {}
160 if (posix_openpt(flags: 0) != -1) {}
161 if (posix_openpt(flags: 0) <= -1) {}
162 if (posix_openpt(flags: 0) < -1) {}
163 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) <= 0) {}
164 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) == 1) {}
165}
166
167namespace i {
168int posix_fadvise(int fd, off_t offset, off_t len, int advice);
169int pthread_yield(void);
170
171void noWarning() {
172 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) < 0) {}
173 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) >= 0) {}
174 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) == -1) {}
175 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) != -1) {}
176 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) <= -1) {}
177 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) < -1) {}
178 if (pthread_yield() < 0) {}
179 if (pthread_yield() >= 0) {}
180 if (pthread_yield() == -1) {}
181 if (pthread_yield() != -1) {}
182 if (pthread_yield() <= -1) {}
183 if (pthread_yield() < -1) {}
184}
185
186} // namespace i
187
188class G {
189 public:
190 int posix_fadvise(int fd, off_t offset, off_t len, int advice);
191 int pthread_yield(void);
192
193 void noWarning() {
194 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) < 0) {}
195 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) >= 0) {}
196 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) == -1) {}
197 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) != -1) {}
198 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) <= -1) {}
199 if (posix_fadvise(fd: 0, offset: 0, len: 0, advice: 0) < -1) {}
200 if (pthread_yield() < 0) {}
201 if (pthread_yield() >= 0) {}
202 if (pthread_yield() == -1) {}
203 if (pthread_yield() != -1) {}
204 if (pthread_yield() <= -1) {}
205 if (pthread_yield() < -1) {}
206 }
207};
208

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/posix-return.cpp