diff options
author | Chih-Hung Hsieh <chh@google.com> | 2017-08-14 17:45:48 +0000 |
---|---|---|
committer | Chih-Hung Hsieh <chh@google.com> | 2017-08-14 17:45:48 +0000 |
commit | 7651e66cdfd24425f0fcaadf3cc892a47e6cb37e (patch) | |
tree | d0fb06c88654986b9777d153aaee2fdf10ff9ef6 /clang-tools-extra/test/clang-tidy/android-cloexec-inotify-init1.cpp | |
parent | 2e6f9a16f936d3ef754cdb1bd0dd59a8680f374d (diff) | |
download | bcm5719-llvm-7651e66cdfd24425f0fcaadf3cc892a47e6cb37e.tar.gz bcm5719-llvm-7651e66cdfd24425f0fcaadf3cc892a47e6cb37e.zip |
[clang-tidy] Add a close-on-exec check on inotify_init1() in Android module.
Summary:
inotify_init1() is better to set IN_CLOEXEC flag to avoid file descriptor leakage.
Differential Revision: https://reviews.llvm.org/D35368
llvm-svn: 310863
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/android-cloexec-inotify-init1.cpp')
-rw-r--r-- | clang-tools-extra/test/clang-tidy/android-cloexec-inotify-init1.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/android-cloexec-inotify-init1.cpp b/clang-tools-extra/test/clang-tidy/android-cloexec-inotify-init1.cpp new file mode 100644 index 00000000000..2b74fad1359 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/android-cloexec-inotify-init1.cpp @@ -0,0 +1,64 @@ +// RUN: %check_clang_tidy %s android-cloexec-inotify-init1 %t + +#define IN_NONBLOCK 1 +#define __O_CLOEXEC 3 +#define IN_CLOEXEC __O_CLOEXEC +#define TEMP_FAILURE_RETRY(exp) \ + ({ \ + int _rc; \ + do { \ + _rc = (exp); \ + } while (_rc == -1); \ + }) + +extern "C" int inotify_init1(int flags); + +void a() { + inotify_init1(IN_NONBLOCK); + // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: 'inotify_init1' should use IN_CLOEXEC where possible [android-cloexec-inotify-init1] + // CHECK-FIXES: inotify_init1(IN_NONBLOCK | IN_CLOEXEC); + TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK)); + // CHECK-MESSAGES: :[[@LINE-1]]:47: warning: 'inotify_init1' + // CHECK-FIXES: TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK | IN_CLOEXEC)); +} + +void f() { + inotify_init1(0); + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'inotify_init1' + // CHECK-FIXES: inotify_init1(IN_CLOEXEC); + TEMP_FAILURE_RETRY(inotify_init1(0)); + // CHECK-MESSAGES: :[[@LINE-1]]:37: warning: 'inotify_init1' + // CHECK-FIXES: TEMP_FAILURE_RETRY(inotify_init1(IN_CLOEXEC)); + + int flag = 1; + inotify_init1(flag); + TEMP_FAILURE_RETRY(inotify_init1(flag)); +} + +namespace i { +int inotify_init1(int flags); + +void d() { + inotify_init1(IN_NONBLOCK); + TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK)); +} + +} // namespace i + +void e() { + inotify_init1(IN_CLOEXEC); + TEMP_FAILURE_RETRY(inotify_init1(IN_CLOEXEC)); + inotify_init1(IN_NONBLOCK | IN_CLOEXEC); + TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK | IN_CLOEXEC)); +} + +class G { +public: + int inotify_init1(int flags); + void d() { + inotify_init1(IN_CLOEXEC); + TEMP_FAILURE_RETRY(inotify_init1(IN_CLOEXEC)); + inotify_init1(IN_NONBLOCK | IN_CLOEXEC); + TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK | IN_CLOEXEC)); + } +}; |