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

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