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 
7 typedef int pid_t;
8 typedef long off_t;
9 typedef decltype(sizeof(int)) size_t;
10 typedef struct __posix_spawn_file_actions* posix_spawn_file_actions_t;
11 typedef struct __posix_spawnattr* posix_spawnattr_t;
12 # define __CPU_SETSIZE 1024
13 # define __NCPUBITS (8 * sizeof (__cpu_mask))
14 typedef unsigned long int __cpu_mask;
15 typedef struct
16 {
17   __cpu_mask __bits[__CPU_SETSIZE / __NCPUBITS];
18 } cpu_set_t;
19 typedef struct _opaque_pthread_t *__darwin_pthread_t;
20 typedef __darwin_pthread_t pthread_t;
21 typedef struct pthread_attr_t_ *pthread_attr_t;
22 
23 extern "C" int posix_fadvise(int fd, off_t offset, off_t len, int advice);
24 extern "C" int posix_fallocate(int fd, off_t offset, off_t len);
25 extern "C" int posix_madvise(void *addr, size_t len, int advice);
26 extern "C" int posix_memalign(void **memptr, size_t alignment, size_t size);
27 extern "C" int posix_openpt(int flags);
28 extern "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[]);
32 extern "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[]);
36 extern "C" int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
37 extern "C" int pthread_attr_setaffinity_np(pthread_attr_t *attr, size_t cpusetsize, const cpu_set_t *cpuset);
38 extern "C" int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
39 extern "C" int pthread_attr_init(pthread_attr_t *attr);
40 extern "C" int pthread_yield(void);
41 
42 
warningLessThanZero()43 void warningLessThanZero() {
44   if (posix_fadvise(0, 0, 0, 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(0, 0, 0) < 0) {}
48   // CHECK-MESSAGES: :[[@LINE-1]]:32: warning:
49   // CHECK-FIXES: posix_fallocate(0, 0, 0) > 0
50   if (posix_madvise(NULL, 0, 0) < 0) {}
51   // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
52   // CHECK-FIXES: posix_madvise(NULL, 0, 0) > 0
53   if (posix_memalign(NULL, 0, 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, {NULL}, {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, {NULL}, {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, 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, 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 
warningAlwaysTrue()80 void warningAlwaysTrue() {
81   if (posix_fadvise(0, 0, 0, 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, 0, NULL) >= 0) {}
86   // CHECK-MESSAGES: :[[@LINE-1]]:50: warning:
87   if (pthread_attr_setschedpolicy(NULL, 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 
warningEqualsNegative()96 void warningEqualsNegative() {
97   if (posix_fadvise(0, 0, 0, 0) == -1) {}
98   // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: posix_fadvise
99   if (posix_fadvise(0, 0, 0, 0) != -1) {}
100   // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
101   if (posix_fadvise(0, 0, 0, 0) <= -1) {}
102   // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
103   if (posix_fadvise(0, 0, 0, 0) < -1) {}
104   // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
105   if (posix_fallocate(0, 0, 0) == -1) {}
106   // CHECK-MESSAGES: :[[@LINE-1]]:32: warning:
107   if (posix_madvise(NULL, 0, 0) == -1) {}
108   // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
109   if (posix_memalign(NULL, 0, 0) == -1) {}
110   // CHECK-MESSAGES: :[[@LINE-1]]:34: warning:
111   if (posix_spawn(NULL, NULL, NULL, NULL, {NULL}, {NULL}) == -1) {}
112   // CHECK-MESSAGES: :[[@LINE-1]]:59: warning:
113   if (posix_spawnp(NULL, NULL, NULL, NULL, {NULL}, {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 
WarningWithMacro()126 void WarningWithMacro() {
127   if (posix_fadvise(0, 0, 0, 0) < ZERO) {}
128   // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
129   // CHECK-FIXES: posix_fadvise(0, 0, 0, 0) > ZERO
130   if (posix_fadvise(0, 0, 0, 0) >= ZERO) {}
131   // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
132   if (posix_fadvise(0, 0, 0, 0) == NEGATIVE_ONE) {}
133   // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
134   if (posix_fadvise(0, 0, 0, 0) != NEGATIVE_ONE) {}
135   // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
136   if (posix_fadvise(0, 0, 0, 0) <= NEGATIVE_ONE) {}
137   // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
138   if (posix_fadvise(0, 0, 0, 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 
noWarning()156 void noWarning() {
157   if (posix_openpt(0) < 0) {}
158   if (posix_openpt(0) <= 0) {}
159   if (posix_openpt(0) == -1) {}
160   if (posix_openpt(0) != -1) {}
161   if (posix_openpt(0) <= -1) {}
162   if (posix_openpt(0) < -1) {}
163   if (posix_fadvise(0, 0, 0, 0) <= 0) {}
164   if (posix_fadvise(0, 0, 0, 0) == 1) {}
165 }
166 
167 namespace i {
168 int posix_fadvise(int fd, off_t offset, off_t len, int advice);
169 int pthread_yield(void);
170 
noWarning()171 void noWarning() {
172   if (posix_fadvise(0, 0, 0, 0) < 0) {}
173   if (posix_fadvise(0, 0, 0, 0) >= 0) {}
174   if (posix_fadvise(0, 0, 0, 0) == -1) {}
175   if (posix_fadvise(0, 0, 0, 0) != -1) {}
176   if (posix_fadvise(0, 0, 0, 0) <= -1) {}
177   if (posix_fadvise(0, 0, 0, 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 
188 class G {
189  public:
190   int posix_fadvise(int fd, off_t offset, off_t len, int advice);
191   int pthread_yield(void);
192 
noWarning()193   void noWarning() {
194     if (posix_fadvise(0, 0, 0, 0) < 0) {}
195     if (posix_fadvise(0, 0, 0, 0) >= 0) {}
196     if (posix_fadvise(0, 0, 0, 0) == -1) {}
197     if (posix_fadvise(0, 0, 0, 0) != -1) {}
198     if (posix_fadvise(0, 0, 0, 0) <= -1) {}
199     if (posix_fadvise(0, 0, 0, 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