From 46bc30472bd98182562446be98c4f27133a59a74 Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Mon, 5 Oct 2015 20:08:59 +0000 Subject: [PATCH] Adding a checker (cert-dcl50-cpp) that detects the definition of a C-style variadic function in C++ code. Corresponds to the CERT C++ secure coding rule: https://www.securecoding.cert.org/confluence/display/cplusplus/DCL50-CPP.+Do+not+define+a+C-style+variadic+function llvm-svn: 249343 --- .../clang-tidy/cert/CERTTidyModule.cpp | 3 ++ .../clang-tidy/cert/CMakeLists.txt | 1 + .../cert/VariadicFunctionDefCheck.cpp | 38 +++++++++++++++++++ .../cert/VariadicFunctionDefCheck.h | 34 +++++++++++++++++ .../checks/cert-variadic-function-def.rst | 13 +++++++ .../docs/clang-tidy/checks/list.rst | 1 + .../clang-tidy/cert-variadic-function-def.cpp | 18 +++++++++ 7 files changed, 108 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/cert/VariadicFunctionDefCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/cert/VariadicFunctionDefCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/cert-variadic-function-def.rst create mode 100644 clang-tools-extra/test/clang-tidy/cert-variadic-function-def.cpp diff --git a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp index 0c6b5dca0413..d73e21470fe1 100644 --- a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp @@ -15,6 +15,7 @@ #include "../misc/NewDeleteOverloadsCheck.h" #include "../misc/NonCopyableObjects.h" #include "../misc/StaticAssertCheck.h" +#include "VariadicFunctionDefCheck.h" namespace clang { namespace tidy { @@ -25,6 +26,8 @@ public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { // C++ checkers // DCL + CheckFactories.registerCheck( + "cert-dcl50-cpp"); CheckFactories.registerCheck( "cert-dcl54-cpp"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt index 702885f8b5a5..ce5c2c9592a7 100644 --- a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt @@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyCERTModule CERTTidyModule.cpp + VariadicFunctionDefCheck.cpp LINK_LIBS clangAST diff --git a/clang-tools-extra/clang-tidy/cert/VariadicFunctionDefCheck.cpp b/clang-tools-extra/clang-tidy/cert/VariadicFunctionDefCheck.cpp new file mode 100644 index 000000000000..ac788a3aee5c --- /dev/null +++ b/clang-tools-extra/clang-tidy/cert/VariadicFunctionDefCheck.cpp @@ -0,0 +1,38 @@ +//===--- VariadicfunctiondefCheck.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 "VariadicFunctionDefCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { + +void VariadicFunctionDefCheck::registerMatchers(MatchFinder *Finder) { + if (!getLangOpts().CPlusPlus) + return; + + // We only care about function *definitions* that are variadic. + Finder->addMatcher(functionDecl(isDefinition(), isVariadic()).bind("func"), + this); +} + +void VariadicFunctionDefCheck::check(const MatchFinder::MatchResult &Result) { + const auto *FD = Result.Nodes.getNodeAs("func"); + + diag(FD->getLocation(), + "do not define a C-style variadic function; consider using a function " + "parameter pack or currying instead"); +} + +} // namespace tidy +} // namespace clang + diff --git a/clang-tools-extra/clang-tidy/cert/VariadicFunctionDefCheck.h b/clang-tools-extra/clang-tidy/cert/VariadicFunctionDefCheck.h new file mode 100644 index 000000000000..9ee868331047 --- /dev/null +++ b/clang-tools-extra/clang-tidy/cert/VariadicFunctionDefCheck.h @@ -0,0 +1,34 @@ +//===--- VariadicFunctionDefCheck.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_CERT_VARIADICFUNCTIONDEF_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_VARIADICFUNCTIONDEF_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { + +/// Guards against any C-style variadic function definitions (not declarations). +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/cert-variadic-function-def.html +class VariadicFunctionDefCheck : public ClangTidyCheck { +public: + VariadicFunctionDefCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_VARIADICFUNCTIONDEF_H + diff --git a/clang-tools-extra/docs/clang-tidy/checks/cert-variadic-function-def.rst b/clang-tools-extra/docs/clang-tidy/checks/cert-variadic-function-def.rst new file mode 100644 index 000000000000..ece96746bc51 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/cert-variadic-function-def.rst @@ -0,0 +1,13 @@ +cert-dcl50-cpp +======================== + +A variadic function using an ellipsis has no mechanisms to check the type safety +of arguments being passed to the function or to check that the number of +arguments being passed matches the semantics of the function definition. +Consequently, a runtime call to a C-style variadic function that passes +inappropriate arguments yields undefined behavior. Such undefined behavior could +be exploited to run arbitrary code. + +This check corresponds to the CERT C++ Coding Standard rule +`DCL50-CPP. Do not define a C-style variadic function +`_. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 497670e10b99..03a9af8b3e75 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -2,6 +2,7 @@ List of clang-tidy Checks ========================= .. toctree:: + cert-variadic-function-def google-build-explicit-make-pair google-build-namespaces google-build-using-namespace diff --git a/clang-tools-extra/test/clang-tidy/cert-variadic-function-def.cpp b/clang-tools-extra/test/clang-tidy/cert-variadic-function-def.cpp new file mode 100644 index 000000000000..6bb6b1371b86 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/cert-variadic-function-def.cpp @@ -0,0 +1,18 @@ +// RUN: %python %S/check_clang_tidy.py %s cert-dcl50-cpp %t + +// Variadic function definitions are diagnosed. +void f1(int, ...) {} +// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: do not define a C-style variadic function; consider using a function parameter pack or currying instead [cert-dcl50-cpp] + +// Variadic function *declarations* are not diagnosed. +void f2(int, ...); // ok + +// Function parameter packs are good, however. +template +void f3(Arg F, Ts... Rest) {} + +struct S { + void f(int, ...); // ok + void f1(int, ...) {} + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: do not define a C-style variadic function; consider using a function parameter pack or currying instead +};