diff options
author | George Burgess IV <george.burgess.iv@gmail.com> | 2019-06-06 05:21:45 +0000 |
---|---|---|
committer | George Burgess IV <george.burgess.iv@gmail.com> | 2019-06-06 05:21:45 +0000 |
commit | 5b2a85d0ded21097885900889faa871fdb2e71b5 (patch) | |
tree | 040307052a8a0d7a911cf74d7c617a32cfea801c /clang-tools-extra/clang-tidy/android | |
parent | 3da331b456214c51b1735d02d304df91930cd461 (diff) | |
download | llvm-5b2a85d0ded21097885900889faa871fdb2e71b5.zip llvm-5b2a85d0ded21097885900889faa871fdb2e71b5.tar.gz llvm-5b2a85d0ded21097885900889faa871fdb2e71b5.tar.bz2 |
android: add a close-on-exec check on pipe()
On Android, pipe() is better to be replaced by pipe2() with O_CLOEXEC
flag to avoid file descriptor leakage.
Patch by Jian Cai!
Differential Revision: https://reviews.llvm.org/D61967
llvm-svn: 362673
Diffstat (limited to 'clang-tools-extra/clang-tidy/android')
4 files changed, 74 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp b/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp index 237d5ff..e978309 100644 --- a/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp @@ -20,6 +20,7 @@ #include "CloexecInotifyInitCheck.h" #include "CloexecMemfdCreateCheck.h" #include "CloexecOpenCheck.h" +#include "CloexecPipeCheck.h" #include "CloexecPipe2Check.h" #include "CloexecSocketCheck.h" #include "ComparisonInTempFailureRetryCheck.h" @@ -50,6 +51,7 @@ public: CheckFactories.registerCheck<CloexecMemfdCreateCheck>( "android-cloexec-memfd-create"); CheckFactories.registerCheck<CloexecOpenCheck>("android-cloexec-open"); + CheckFactories.registerCheck<CloexecPipeCheck>("android-cloexec-pipe"); CheckFactories.registerCheck<CloexecPipe2Check>("android-cloexec-pipe2"); CheckFactories.registerCheck<CloexecSocketCheck>("android-cloexec-socket"); CheckFactories.registerCheck<ComparisonInTempFailureRetryCheck>( diff --git a/clang-tools-extra/clang-tidy/android/CMakeLists.txt b/clang-tools-extra/clang-tidy/android/CMakeLists.txt index a89d7a4..9d04003 100644 --- a/clang-tools-extra/clang-tidy/android/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/android/CMakeLists.txt @@ -14,6 +14,7 @@ add_clang_library(clangTidyAndroidModule CloexecInotifyInitCheck.cpp CloexecMemfdCreateCheck.cpp CloexecOpenCheck.cpp + CloexecPipeCheck.cpp CloexecPipe2Check.cpp CloexecSocketCheck.cpp ComparisonInTempFailureRetryCheck.cpp diff --git a/clang-tools-extra/clang-tidy/android/CloexecPipeCheck.cpp b/clang-tools-extra/clang-tidy/android/CloexecPipeCheck.cpp new file mode 100644 index 0000000..94b9450 --- /dev/null +++ b/clang-tools-extra/clang-tidy/android/CloexecPipeCheck.cpp @@ -0,0 +1,37 @@ +//===--- CloexecPipeCheck.cpp - clang-tidy---------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "CloexecPipeCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace android { + +void CloexecPipeCheck::registerMatchers(MatchFinder *Finder) { + registerMatchersImpl(Finder, + functionDecl(returns(isInteger()), hasName("pipe"), + hasParameter(0, hasType(pointsTo(isInteger()))))); +} + +void CloexecPipeCheck::check(const MatchFinder::MatchResult &Result) { + std::string ReplacementText = + (Twine("pipe2(") + getSpellingArg(Result, 0) + ", O_CLOEXEC)").str(); + + replaceFunc( + Result, + "prefer pipe2() with O_CLOEXEC to avoid leaking file descriptors to child processes", + ReplacementText); +} + +} // namespace android +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/android/CloexecPipeCheck.h b/clang-tools-extra/clang-tidy/android/CloexecPipeCheck.h new file mode 100644 index 0000000..c89fe79 --- /dev/null +++ b/clang-tools-extra/clang-tidy/android/CloexecPipeCheck.h @@ -0,0 +1,34 @@ +//===--- CloexecPipeCheck.h - clang-tidy-------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_PIPE_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_PIPE_H + +#include "CloexecCheck.h" + +namespace clang { +namespace tidy { +namespace android { + +/// Suggests to replace calls to pipe() with calls to pipe2(). +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-pipe.html +class CloexecPipeCheck : public CloexecCheck { +public: + CloexecPipeCheck(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_PIPE_H |