[clang-tools-extra] r311029 - [clang-tidy] Add a close-on-exec check on epoll_create() in Android module.

Chih-Hung Hsieh via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 16 11:02:49 PDT 2017


Author: chh
Date: Wed Aug 16 11:02:49 2017
New Revision: 311029

URL: http://llvm.org/viewvc/llvm-project?rev=311029&view=rev
Log:
[clang-tidy] Add a close-on-exec check on epoll_create() in Android module.

Summary:
epoll_create() is better to be replaced by epoll_create1() with EPOLL_CLOEXEC
flag to avoid file descriptor leakage.

Differential Revision: https://reviews.llvm.org/D35367


Added:
    clang-tools-extra/trunk/clang-tidy/android/CloexecEpollCreateCheck.cpp
    clang-tools-extra/trunk/clang-tidy/android/CloexecEpollCreateCheck.h
    clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-epoll-create.rst
    clang-tools-extra/trunk/test/clang-tidy/android-cloexec-epoll-create.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=311029&r1=311028&r2=311029&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp Wed Aug 16 11:02:49 2017
@@ -14,6 +14,7 @@
 #include "CloexecAcceptCheck.h"
 #include "CloexecCreatCheck.h"
 #include "CloexecEpollCreate1Check.h"
+#include "CloexecEpollCreateCheck.h"
 #include "CloexecDupCheck.h"
 #include "CloexecFopenCheck.h"
 #include "CloexecInotifyInit1Check.h"
@@ -37,6 +38,8 @@ public:
     CheckFactories.registerCheck<CloexecCreatCheck>("android-cloexec-creat");
     CheckFactories.registerCheck<CloexecEpollCreate1Check>(
         "android-cloexec-epoll-create1");
+    CheckFactories.registerCheck<CloexecEpollCreateCheck>(
+        "android-cloexec-epoll-create");
     CheckFactories.registerCheck<CloexecDupCheck>("android-cloexec-dup");
     CheckFactories.registerCheck<CloexecFopenCheck>("android-cloexec-fopen");
     CheckFactories.registerCheck<CloexecInotifyInitCheck>(

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=311029&r1=311028&r2=311029&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt Wed Aug 16 11:02:49 2017
@@ -7,6 +7,7 @@ add_clang_library(clangTidyAndroidModule
   CloexecCheck.cpp
   CloexecCreatCheck.cpp
   CloexecEpollCreate1Check.cpp
+  CloexecEpollCreateCheck.cpp
   CloexecDupCheck.cpp
   CloexecFopenCheck.cpp
   CloexecInotifyInit1Check.cpp

Added: clang-tools-extra/trunk/clang-tidy/android/CloexecEpollCreateCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecEpollCreateCheck.cpp?rev=311029&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CloexecEpollCreateCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/android/CloexecEpollCreateCheck.cpp Wed Aug 16 11:02:49 2017
@@ -0,0 +1,36 @@
+//===--- CloexecEpollCreateCheck.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 "CloexecEpollCreateCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace android {
+
+void CloexecEpollCreateCheck::registerMatchers(MatchFinder *Finder) {
+  registerMatchersImpl(
+      Finder, functionDecl(returns(isInteger()), hasName("epoll_create"),
+                           hasParameter(0, hasType(isInteger()))));
+}
+
+void CloexecEpollCreateCheck::check(const MatchFinder::MatchResult &Result) {
+  replaceFunc(Result,
+              "prefer epoll_create() to epoll_create1() "
+              "because epoll_create1() allows "
+              "EPOLL_CLOEXEC",
+              "epoll_create1(EPOLL_CLOEXEC)");
+}
+
+} // namespace android
+} // namespace tidy
+} // namespace clang

Added: clang-tools-extra/trunk/clang-tidy/android/CloexecEpollCreateCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecEpollCreateCheck.h?rev=311029&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CloexecEpollCreateCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/android/CloexecEpollCreateCheck.h Wed Aug 16 11:02:49 2017
@@ -0,0 +1,35 @@
+//===--- CloexecEpollCreateCheck.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_EPOLL_CREATE_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_EPOLL_CREATE_H
+
+#include "CloexecCheck.h"
+
+namespace clang {
+namespace tidy {
+namespace android {
+
+/// epoll_create() is better to be replaced by epoll_create1().
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-epoll-create.html
+class CloexecEpollCreateCheck : public CloexecCheck {
+public:
+  CloexecEpollCreateCheck(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_EPOLL_CREATE_H

Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=311029&r1=311028&r2=311029&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Wed Aug 16 11:02:49 2017
@@ -97,6 +97,11 @@ Improvements to clang-tidy
   Checks if the required file flag ``EPOLL_CLOEXEC`` is present in the argument of
   ``epoll_create1()``.
 
+- New `android-cloexec-epoll-create
+  <http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-epoll-create.html>`_ check
+
+  Detects usage of ``epoll_create()``.
+
 - 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-epoll-create.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-epoll-create.rst?rev=311029&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-epoll-create.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-epoll-create.rst Wed Aug 16 11:02:49 2017
@@ -0,0 +1,17 @@
+.. title:: clang-tidy - android-cloexec-epoll-create
+
+android-cloexec-epoll-create
+============================
+
+The usage of ``epoll_create()`` is not recommended, it's better to use
+``epoll_create1()``, which allows close-on-exec.
+
+Examples:
+
+.. code-block:: c++
+
+  epoll_create(size);
+
+  // becomes
+
+  epoll_create1(EPOLL_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=311029&r1=311028&r2=311029&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Wed Aug 16 11:02:49 2017
@@ -7,6 +7,7 @@ Clang-Tidy Checks
    android-cloexec-accept
    android-cloexec-accept4
    android-cloexec-creat
+   android-cloexec-epoll-create
    android-cloexec-epoll-create1
    android-cloexec-dup
    android-cloexec-fopen

Added: clang-tools-extra/trunk/test/clang-tidy/android-cloexec-epoll-create.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/android-cloexec-epoll-create.cpp?rev=311029&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/android-cloexec-epoll-create.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/android-cloexec-epoll-create.cpp Wed Aug 16 11:02:49 2017
@@ -0,0 +1,24 @@
+// RUN: %check_clang_tidy %s android-cloexec-epoll-create %t
+
+extern "C" int epoll_create(int size);
+
+void f() {
+  epoll_create(0);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: prefer epoll_create() to epoll_create1() because epoll_create1() allows EPOLL_CLOEXEC [android-cloexec-epoll-create]
+  // CHECK-FIXES: epoll_create1(EPOLL_CLOEXEC);
+}
+
+namespace i {
+int epoll_create(int size);
+void g() {
+  epoll_create(0);
+}
+} // namespace i
+
+class C {
+public:
+  int epoll_create(int size);
+  void h() {
+    epoll_create(0);
+  }
+};




More information about the cfe-commits mailing list