Author: chh Date: Mon Aug 14 10:25:41 2017 New Revision: 310861 URL: http://llvm.org/viewvc/llvm-project?rev=310861&view=rev Log: [clang-tidy] Add a close-on-exec check on inotify_init() in Android module.
Summary: inotify_init() is better to be replaced by inotify_init1() with IN_CLOEXEC flag to avoid file descriptor leakage. Differential Revision: https://reviews.llvm.org/D35370 Added: clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInitCheck.cpp clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInitCheck.h clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-inotify-init.rst clang-tools-extra/trunk/test/clang-tidy/android-cloexec-inotify-init.cpp Modified: clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt clang-tools-extra/trunk/docs/ReleaseNotes.rst clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Modified: clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp?rev=310861&r1=310860&r2=310861&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp Mon Aug 14 10:25:41 2017 @@ -13,6 +13,7 @@ #include "CloexecCreatCheck.h" #include "CloexecDupCheck.h" #include "CloexecFopenCheck.h" +#include "CloexecInotifyInitCheck.h" #include "CloexecMemfdCreateCheck.h" #include "CloexecOpenCheck.h" #include "CloexecSocketCheck.h" @@ -30,6 +31,8 @@ public: CheckFactories.registerCheck<CloexecCreatCheck>("android-cloexec-creat"); CheckFactories.registerCheck<CloexecDupCheck>("android-cloexec-dup"); CheckFactories.registerCheck<CloexecFopenCheck>("android-cloexec-fopen"); + CheckFactories.registerCheck<CloexecInotifyInitCheck>( + "android-cloexec-inotify-init"); CheckFactories.registerCheck<CloexecMemfdCreateCheck>( "android-cloexec-memfd-create"); CheckFactories.registerCheck<CloexecOpenCheck>("android-cloexec-open"); Modified: clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt?rev=310861&r1=310860&r2=310861&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt (original) +++ clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt Mon Aug 14 10:25:41 2017 @@ -6,6 +6,7 @@ add_clang_library(clangTidyAndroidModule CloexecCreatCheck.cpp CloexecDupCheck.cpp CloexecFopenCheck.cpp + CloexecInotifyInitCheck.cpp CloexecMemfdCreateCheck.cpp CloexecOpenCheck.cpp CloexecSocketCheck.cpp Added: clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInitCheck.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInitCheck.cpp?rev=310861&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInitCheck.cpp (added) +++ clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInitCheck.cpp Mon Aug 14 10:25:41 2017 @@ -0,0 +1,34 @@ +//===--- CloexecInotifyInitCheck.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 "CloexecInotifyInitCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace android { + +void CloexecInotifyInitCheck::registerMatchers(MatchFinder *Finder) { + registerMatchersImpl( + Finder, functionDecl(returns(isInteger()), hasName("inotify_init"))); +} + +void CloexecInotifyInitCheck::check(const MatchFinder::MatchResult &Result) { + replaceFunc(Result, /*WarningMsg=*/ + "prefer inotify_init() to inotify_init1() " + "because inotify_init1() allows IN_CLOEXEC", + /*FixMsg=*/"inotify_init1(IN_CLOEXEC)"); +} + +} // namespace android +} // namespace tidy +} // namespace clang Added: clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInitCheck.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInitCheck.h?rev=310861&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInitCheck.h (added) +++ clang-tools-extra/trunk/clang-tidy/android/CloexecInotifyInitCheck.h Mon Aug 14 10:25:41 2017 @@ -0,0 +1,35 @@ +//===--- CloexecInotifyInitCheck.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_INIT_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_INOTIFY_INIT_H + +#include "CloexecCheck.h" + +namespace clang { +namespace tidy { +namespace android { + +/// inotify_init() is better to be replaced by inotify_init1(). +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-inotify-init.html +class CloexecInotifyInitCheck : public CloexecCheck { +public: + CloexecInotifyInitCheck(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_INIT_H Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=310861&r1=310860&r2=310861&view=diff ============================================================================== --- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original) +++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Mon Aug 14 10:25:41 2017 @@ -75,6 +75,11 @@ Improvements to clang-tidy Detects usage of ``dup()``. +- New `android-cloexec-inotify-init + <http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-inotify-init.html>`_ check + + Detects usage of ``inotify_init()``. + - New `android-cloexec-memfd_create <http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-memfd_create.html>`_ check Added: clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-inotify-init.rst URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-inotify-init.rst?rev=310861&view=auto ============================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-inotify-init.rst (added) +++ clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-inotify-init.rst Mon Aug 14 10:25:41 2017 @@ -0,0 +1,17 @@ +.. title:: clang-tidy - android-cloexec-inotify-init + +android-cloexec-inotify-init +============================ + +The usage of ``inotify_init()`` is not recommended, it's better to use +``inotify_init1()``. + +Examples: + +.. code-block:: c++ + + inotify_init(); + + // becomes + + inotify_init1(IN_CLOEXEC); Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst?rev=310861&r1=310860&r2=310861&view=diff ============================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original) +++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Mon Aug 14 10:25:41 2017 @@ -7,6 +7,7 @@ Clang-Tidy Checks android-cloexec-creat android-cloexec-dup android-cloexec-fopen + android-cloexec-inotify-init android-cloexec-memfd-create android-cloexec-open android-cloexec-socket Added: clang-tools-extra/trunk/test/clang-tidy/android-cloexec-inotify-init.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/android-cloexec-inotify-init.cpp?rev=310861&view=auto ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/android-cloexec-inotify-init.cpp (added) +++ clang-tools-extra/trunk/test/clang-tidy/android-cloexec-inotify-init.cpp Mon Aug 14 10:25:41 2017 @@ -0,0 +1,24 @@ +// RUN: %check_clang_tidy %s android-cloexec-inotify-init %t + +extern "C" int inotify_init(); + +void f() { + inotify_init(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: prefer inotify_init() to inotify_init1() because inotify_init1() allows IN_CLOEXEC [android-cloexec-inotify-init] + // CHECK-FIXES: inotify_init1(IN_CLOEXEC); +} + +namespace i { +int inotify_init(); +void g() { + inotify_init(); +} +} // namespace i + +class C { +public: + int inotify_init(); + void h() { + inotify_init(); + } +}; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits