1// RUN: %check_clang_tidy %s android-cloexec-accept4 %t
2
3typedef int socklen_t;
4struct sockaddr {};
5
6#define SOCK_NONBLOCK 1
7#define __O_CLOEXEC 3
8#define SOCK_CLOEXEC __O_CLOEXEC
9#define TEMP_FAILURE_RETRY(exp) \
10 ({ \
11 int _rc; \
12 do { \
13 _rc = (exp); \
14 } while (_rc == -1); \
15 })
16#define NULL 0
17
18extern "C" int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
19
20void a() {
21 accept4(sockfd: 0, NULL, NULL, SOCK_NONBLOCK);
22 // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: 'accept4' should use SOCK_CLOEXEC where possible [android-cloexec-accept4]
23 // CHECK-FIXES: accept4(0, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);
24 TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, SOCK_NONBLOCK));
25 // CHECK-MESSAGES: :[[@LINE-1]]:58: warning: 'accept4'
26 // CHECK-FIXES: TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC));
27}
28
29void f() {
30 accept4(sockfd: 0, NULL, NULL, flags: 3);
31 // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 'accept4'
32 // CHECK-FIXES: accept4(0, NULL, NULL, 3 | SOCK_CLOEXEC);
33 TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, 3));
34 // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: 'accept4'
35 // CHECK-FIXES: TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, 3 | SOCK_CLOEXEC));
36
37 int flag = SOCK_NONBLOCK;
38 accept4(sockfd: 0, NULL, NULL, flags: flag);
39 TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, flag));
40}
41
42namespace i {
43int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
44
45void d() {
46 accept4(sockfd: 0, NULL, NULL, SOCK_NONBLOCK);
47 TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, SOCK_NONBLOCK));
48}
49
50} // namespace i
51
52void e() {
53 accept4(sockfd: 0, NULL, NULL, SOCK_CLOEXEC);
54 TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, SOCK_CLOEXEC));
55 accept4(sockfd: 0, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);
56 TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC));
57}
58
59class G {
60public:
61 int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
62 void d() {
63 accept4(sockfd: 0, NULL, NULL, SOCK_NONBLOCK);
64 TEMP_FAILURE_RETRY(accept4(0, NULL, NULL, SOCK_NONBLOCK));
65 }
66};
67

source code of clang-tools-extra/test/clang-tidy/checkers/android/cloexec-accept4.cpp