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/clang-tidy | |
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/clang-tidy')
4 files changed, 72 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp b/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp index 316d228a7b0..aca2d5f1ae5 100644 --- a/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp @@ -13,6 +13,7 @@ #include "CloexecCreatCheck.h" #include "CloexecDupCheck.h" #include "CloexecFopenCheck.h" +#include "CloexecInotifyInit1Check.h" #include "CloexecInotifyInitCheck.h" #include "CloexecMemfdCreateCheck.h" #include "CloexecOpenCheck.h" @@ -33,6 +34,8 @@ public: CheckFactories.registerCheck<CloexecFopenCheck>("android-cloexec-fopen"); CheckFactories.registerCheck<CloexecInotifyInitCheck>( "android-cloexec-inotify-init"); + CheckFactories.registerCheck<CloexecInotifyInit1Check>( + "android-cloexec-inotify-init1"); CheckFactories.registerCheck<CloexecMemfdCreateCheck>( "android-cloexec-memfd-create"); CheckFactories.registerCheck<CloexecOpenCheck>("android-cloexec-open"); diff --git a/clang-tools-extra/clang-tidy/android/CMakeLists.txt b/clang-tools-extra/clang-tidy/android/CMakeLists.txt index 466358a7e92..be345626335 100644 --- a/clang-tools-extra/clang-tidy/android/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/android/CMakeLists.txt @@ -6,6 +6,7 @@ add_clang_library(clangTidyAndroidModule CloexecCreatCheck.cpp CloexecDupCheck.cpp CloexecFopenCheck.cpp + CloexecInotifyInit1Check.cpp CloexecInotifyInitCheck.cpp CloexecMemfdCreateCheck.cpp CloexecOpenCheck.cpp diff --git a/clang-tools-extra/clang-tidy/android/CloexecInotifyInit1Check.cpp b/clang-tools-extra/clang-tidy/android/CloexecInotifyInit1Check.cpp new file mode 100644 index 00000000000..788de19be60 --- /dev/null +++ b/clang-tools-extra/clang-tidy/android/CloexecInotifyInit1Check.cpp @@ -0,0 +1,33 @@ +//===--- CloexecInotifyInit1Check.cpp - clang-tidy-------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "CloexecInotifyInit1Check.h" +#include "../utils/ASTUtils.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace android { + +void CloexecInotifyInit1Check::registerMatchers(MatchFinder *Finder) { + registerMatchersImpl( + Finder, functionDecl(returns(isInteger()), hasName("inotify_init1"), + hasParameter(0, hasType(isInteger())))); +} + +void CloexecInotifyInit1Check::check(const MatchFinder::MatchResult &Result) { + insertMacroFlag(Result, /*MarcoFlag=*/"IN_CLOEXEC", /*ArgPos=*/0); +} + +} // namespace android +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/android/CloexecInotifyInit1Check.h b/clang-tools-extra/clang-tidy/android/CloexecInotifyInit1Check.h new file mode 100644 index 00000000000..deb04f2a382 --- /dev/null +++ b/clang-tools-extra/clang-tidy/android/CloexecInotifyInit1Check.h @@ -0,0 +1,35 @@ +//===--- CloexecInotifyInit1Check.h - clang-tidy-----------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_INOTIFY_INIT1_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_INOTIFY_INIT1_H + +#include "CloexecCheck.h" + +namespace clang { +namespace tidy { +namespace android { + +/// Finds code that uses inotify_init1() without using the IN_CLOEXEC flag. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-inotify-init1.html +class CloexecInotifyInit1Check : public CloexecCheck { +public: + CloexecInotifyInit1Check(StringRef Name, ClangTidyContext *Context) + : CloexecCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace android +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_INOTIFY_INIT1_H |