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 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 if (0 > pthread_yield() ) {} 78 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: 79 // CHECK-FIXES: 0 < pthread_yield() 80 81 } 82 83 void warningAlwaysTrue() { 84 if (posix_fadvise(0, 0, 0, 0) >= 0) {} 85 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: the comparison always evaluates to true because posix_fadvise always returns non-negative values 86 if (pthread_create(NULL, NULL, NULL, NULL) >= 0) {} 87 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: the comparison always evaluates to true because pthread_create always returns non-negative values 88 if (pthread_attr_setaffinity_np(NULL, 0, NULL) >= 0) {} 89 // CHECK-MESSAGES: :[[@LINE-1]]:50: warning: 90 if (pthread_attr_setschedpolicy(NULL, 0) >= 0) {} 91 // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: 92 if (pthread_attr_init(NULL) >= 0) {} 93 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: 94 if (pthread_yield() >= 0) {} 95 // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 96 if (0 <= pthread_yield()) {} 97 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: 98 } 99 100 void warningEqualsNegative() { 101 if (posix_fadvise(0, 0, 0, 0) == -1) {} 102 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: posix_fadvise 103 if (posix_fadvise(0, 0, 0, 0) != -1) {} 104 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: 105 if (posix_fadvise(0, 0, 0, 0) <= -1) {} 106 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: 107 if (posix_fadvise(0, 0, 0, 0) < -1) {} 108 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: 109 if (posix_fallocate(0, 0, 0) == -1) {} 110 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: 111 if (posix_madvise(NULL, 0, 0) == -1) {} 112 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: 113 if (posix_memalign(NULL, 0, 0) == -1) {} 114 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: 115 if (posix_spawn(NULL, NULL, NULL, NULL, {NULL}, {NULL}) == -1) {} 116 // CHECK-MESSAGES: :[[@LINE-1]]:59: warning: 117 if (posix_spawnp(NULL, NULL, NULL, NULL, {NULL}, {NULL}) == -1) {} 118 // CHECK-MESSAGES: :[[@LINE-1]]:60: warning: 119 if (pthread_create(NULL, NULL, NULL, NULL) == -1) {} 120 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: pthread_create 121 if (pthread_create(NULL, NULL, NULL, NULL) != -1) {} 122 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: 123 if (pthread_create(NULL, NULL, NULL, NULL) <= -1) {} 124 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: 125 if (pthread_create(NULL, NULL, NULL, NULL) < -1) {} 126 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: 127 if (-1 == pthread_create(NULL, NULL, NULL, NULL)) {} 128 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 129 if (-1 != pthread_create(NULL, NULL, NULL, NULL)) {} 130 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 131 if (-1 >= pthread_create(NULL, NULL, NULL, NULL)) {} 132 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 133 if (-1 > pthread_create(NULL, NULL, NULL, NULL)) {} 134 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 135 } 136 137 void WarningWithMacro() { 138 if (posix_fadvise(0, 0, 0, 0) < ZERO) {} 139 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: 140 // CHECK-FIXES: posix_fadvise(0, 0, 0, 0) > ZERO 141 if (posix_fadvise(0, 0, 0, 0) >= ZERO) {} 142 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: 143 if (posix_fadvise(0, 0, 0, 0) == NEGATIVE_ONE) {} 144 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: 145 if (posix_fadvise(0, 0, 0, 0) != NEGATIVE_ONE) {} 146 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: 147 if (posix_fadvise(0, 0, 0, 0) <= NEGATIVE_ONE) {} 148 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: 149 if (posix_fadvise(0, 0, 0, 0) < NEGATIVE_ONE) {} 150 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: 151 if (pthread_create(NULL, NULL, NULL, NULL) < ZERO) {} 152 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: 153 // CHECK-FIXES: pthread_create(NULL, NULL, NULL, NULL) > ZERO 154 if (pthread_create(NULL, NULL, NULL, NULL) >= ZERO) {} 155 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: 156 if (pthread_create(NULL, NULL, NULL, NULL) == NEGATIVE_ONE) {} 157 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: 158 if (pthread_create(NULL, NULL, NULL, NULL) != NEGATIVE_ONE) {} 159 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: 160 if (pthread_create(NULL, NULL, NULL, NULL) <= NEGATIVE_ONE) {} 161 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: 162 if (pthread_create(NULL, NULL, NULL, NULL) < NEGATIVE_ONE) {} 163 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: 164 165 } 166 167 void noWarning() { 168 if (posix_openpt(0) < 0) {} 169 if (posix_openpt(0) <= 0) {} 170 if (posix_openpt(0) == -1) {} 171 if (posix_openpt(0) != -1) {} 172 if (posix_openpt(0) <= -1) {} 173 if (posix_openpt(0) < -1) {} 174 if (posix_fadvise(0, 0, 0, 0) <= 0) {} 175 if (posix_fadvise(0, 0, 0, 0) == 1) {} 176 if (0 > posix_openpt(0)) {} 177 if (0 >= posix_openpt(0)) {} 178 if (-1 == posix_openpt(0)) {} 179 if (-1 != posix_openpt(0)) {} 180 if (-1 >= posix_openpt(0)) {} 181 if (-1 > posix_openpt(0)) {} 182 if (posix_fadvise(0, 0, 0, 0) <= 0) {} 183 if (posix_fadvise(0, 0, 0, 0) == 1) {} 184 if (0 >= posix_fadvise(0, 0, 0, 0)) {} 185 if (1 == posix_fadvise(0, 0, 0, 0)) {} 186 } 187 188 namespace i { 189 int posix_fadvise(int fd, off_t offset, off_t len, int advice); 190 int pthread_yield(void); 191 192 void noWarning() { 193 if (posix_fadvise(0, 0, 0, 0) < 0) {} 194 if (posix_fadvise(0, 0, 0, 0) >= 0) {} 195 if (posix_fadvise(0, 0, 0, 0) == -1) {} 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 (pthread_yield() < 0) {} 200 if (pthread_yield() >= 0) {} 201 if (pthread_yield() == -1) {} 202 if (pthread_yield() != -1) {} 203 if (pthread_yield() <= -1) {} 204 if (pthread_yield() < -1) {} 205 } 206 207 } // namespace i 208 209 class G { 210 public: 211 int posix_fadvise(int fd, off_t offset, off_t len, int advice); 212 int pthread_yield(void); 213 214 void noWarning() { 215 if (posix_fadvise(0, 0, 0, 0) < 0) {} 216 if (posix_fadvise(0, 0, 0, 0) >= 0) {} 217 if (posix_fadvise(0, 0, 0, 0) == -1) {} 218 if (posix_fadvise(0, 0, 0, 0) != -1) {} 219 if (posix_fadvise(0, 0, 0, 0) <= -1) {} 220 if (posix_fadvise(0, 0, 0, 0) < -1) {} 221 if (pthread_yield() < 0) {} 222 if (pthread_yield() >= 0) {} 223 if (pthread_yield() == -1) {} 224 if (pthread_yield() != -1) {} 225 if (pthread_yield() <= -1) {} 226 if (pthread_yield() < -1) {} 227 } 228 }; 229