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

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