Index: clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp =================================================================== --- clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -22,6 +22,7 @@ #include "FoldInitTypeCheck.h" #include "ForwardDeclarationNamespaceCheck.h" #include "ForwardingReferenceOverloadCheck.h" +#include "FsetposArgumentCheck.h" #include "InaccurateEraseCheck.h" #include "IncorrectRoundingsCheck.h" #include "InfiniteLoopCheck.h" @@ -94,6 +95,8 @@ "bugprone-forward-declaration-namespace"); CheckFactories.registerCheck( "bugprone-forwarding-reference-overload"); + CheckFactories.registerCheck( + "bugprone-fsetpos-argument-check"); CheckFactories.registerCheck( "bugprone-inaccurate-erase"); CheckFactories.registerCheck( Index: clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt =================================================================== --- clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -17,6 +17,7 @@ FoldInitTypeCheck.cpp ForwardDeclarationNamespaceCheck.cpp ForwardingReferenceOverloadCheck.cpp + FsetposArgumentCheck.cpp InaccurateEraseCheck.cpp IncorrectRoundingsCheck.cpp InfiniteLoopCheck.cpp Index: clang-tools-extra/clang-tidy/bugprone/FsetposArgumentCheck.h =================================================================== --- /dev/null +++ clang-tools-extra/clang-tidy/bugprone/FsetposArgumentCheck.h @@ -0,0 +1,35 @@ +//===--- FsetposArgumentCheck.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_BUGPRONE_FSETPOSARGUMENTCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FSETPOSARGUMENTCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace bugprone { + +/// Finds call of 'fsetpos' functions, which do not have file position indicator +/// obtained from 'fgetpos'. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-fsetpos-argument-check.html +class FsetposArgumentCheck : public ClangTidyCheck { +public: + FsetposArgumentCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace bugprone +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FSETPOSARGUMENTCHECK_H Index: clang-tools-extra/clang-tidy/bugprone/FsetposArgumentCheck.cpp =================================================================== --- /dev/null +++ clang-tools-extra/clang-tidy/bugprone/FsetposArgumentCheck.cpp @@ -0,0 +1,60 @@ +//===--- FsetposArgumentCheck.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 "FsetposArgumentCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace bugprone { + +void FsetposArgumentCheck::registerMatchers(MatchFinder *Finder) { + const auto Offset = declRefExpr(to(varDecl().bind("offset"))); + const auto WithOffset = hasArgument( + 1, + ignoringImpCasts(anyOf(unaryOperator(hasUnaryOperand(Offset)), Offset))); + + const auto IsOffset = declRefExpr(to(varDecl(equalsBoundNode("offset")))); + const auto HasOffset = hasAnyArgument(ignoringImpCasts( + anyOf(unaryOperator(hasUnaryOperand(IsOffset)), IsOffset))); + + const auto OffsetInWrongFunction = hasAncestor(functionDecl( + hasDescendant(callExpr(HasOffset, unless(callee(functionDecl(hasAnyName( + "::fgetpos", "::fsetpos"))))) + .bind("non-fpos")))); + const auto OffsetInFgetpos = hasAncestor(functionDecl(hasDescendant( + callExpr(HasOffset, callee(functionDecl(hasName("::fgetpos"))))))); + + Finder->addMatcher( + callExpr(WithOffset, callee(functionDecl(hasName("::fsetpos"))), + anyOf(OffsetInWrongFunction, unless(OffsetInFgetpos))) + .bind("fsetpos"), + this); +} + +void FsetposArgumentCheck::check(const MatchFinder::MatchResult &Result) { + const auto *MatchedDecl = Result.Nodes.getNodeAs("fsetpos"); + + diag(MatchedDecl->getBeginLoc(), + "file position indicator should be obtained only from 'fgetpos'"); + + const auto *WrongFunction = Result.Nodes.getNodeAs("non-fpos"); + if (WrongFunction) + diag(WrongFunction->getBeginLoc(), + "fpos_t object should be passed as an argument only to 'fsetpos' or " + "'fgetpos' functions.", + DiagnosticIDs::Note); +} + +} // namespace bugprone +} // namespace tidy +} // namespace clang Index: clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp =================================================================== --- clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp +++ clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp @@ -10,6 +10,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "../bugprone/BadSignalToKillThreadCheck.h" +#include "../bugprone/FsetposArgumentCheck.h" #include "../bugprone/ReservedIdentifierCheck.h" #include "../bugprone/SignedCharMisuseCheck.h" #include "../bugprone/SpuriouslyWakeUpFunctionsCheck.h" @@ -100,6 +101,7 @@ CheckFactories.registerCheck("cert-flp30-c"); // FIO CheckFactories.registerCheck("cert-fio38-c"); + CheckFactories.registerCheck("cert-fio44-c"); // ERR CheckFactories.registerCheck("cert-err34-c"); // MSC Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -80,6 +80,12 @@ Finds non-const global variables as described in check I.2 of C++ Core Guidelines. +- New :doc:`bugprone-fsetpos-argument-check + ` check. + + Finds call of ``fsetpos`` functions, which does not have file position indicator + obtained from ``fgetpos``. + - New :doc:`bugprone-misplaced-pointer-arithmetic-in-alloc ` check. @@ -168,6 +174,11 @@ :doc:`bugprone-reserved-identifier ` was added. +- New alias :doc:`cert-fio44-c + ` to + :doc:`bugprone-fsetpos-argument-check + ` was added. + - New alias :doc:`cert-str34-c ` to :doc:`bugprone-signed-char-misuse Index: clang-tools-extra/docs/clang-tidy/checks/bugprone-fsetpos-argument-check.rst =================================================================== --- /dev/null +++ clang-tools-extra/docs/clang-tidy/checks/bugprone-fsetpos-argument-check.rst @@ -0,0 +1,37 @@ +.. title:: clang-tidy - bugprone-fsetpos-argument-check + +bugprone-fsetpos-argument-check +=============================== + +Finds call of ``fsetpos`` functions, which does not have file position indicator +obtained from ``fgetpos``. + +.. code-block:: c + + #include + #include + + int opener(FILE *file) { + int rc; + fpos_t offset; + + memset(&offset, 0, sizeof(offset)); + + if (file == NULL) { + return -1; + } + + /* Read in data from file */ + + rc = fsetpos(file, &offset); + if (rc != 0 ) { + return rc; + } + + return 0; + } + + +This check corresponds to the CERT C Coding Standard rule +`FIO44-C. Only use values for fsetpos() that are returned from fgetpos() +`_. Index: clang-tools-extra/docs/clang-tidy/checks/cert-fio44-c.rst =================================================================== --- /dev/null +++ clang-tools-extra/docs/clang-tidy/checks/cert-fio44-c.rst @@ -0,0 +1,10 @@ +.. title:: clang-tidy - cert-fio44-c +.. meta:: + :http-equiv=refresh: 5;URL=bugprone-fsetpos-argument-check.html + +cert-fio44-c +============ + +The cert-fio44-c check is an alias, please see +`bugprone-fsetpos-argument-check `_ +for more information. Index: clang-tools-extra/docs/clang-tidy/checks/list.rst =================================================================== --- clang-tools-extra/docs/clang-tidy/checks/list.rst +++ clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -57,6 +57,7 @@ `bugprone-fold-init-type `_, `bugprone-forward-declaration-namespace `_, `bugprone-forwarding-reference-overload `_, + `bugprone-fsetpos-argument-check ` _, `bugprone-inaccurate-erase `_, "Yes" `bugprone-incorrect-roundings `_, `bugprone-infinite-loop `_, @@ -315,6 +316,7 @@ `cert-err09-cpp `_, `misc-throw-by-value-catch-by-reference `_, `cert-err61-cpp `_, `misc-throw-by-value-catch-by-reference `_, `cert-fio38-c `_, `misc-non-copyable-objects `_, + `cert-fio44-c `_, `bugprone-fsetpos-argument-check `_, `cert-msc30-c `_, `cert-msc50-cpp `_, `cert-msc32-c `_, `cert-msc51-cpp `_, `cert-oop11-cpp `_, `performance-move-constructor-init `_, "Yes" Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-fsetpos-argument-check.cpp =================================================================== --- /dev/null +++ clang-tools-extra/test/clang-tidy/checkers/bugprone-fsetpos-argument-check.cpp @@ -0,0 +1,59 @@ +// RUN: %check_clang_tidy %s bugprone-fsetpos-argument-check %t + +typedef unsigned int size_t; +class FILE; +struct fpos_t {}; +int fsetpos(FILE *stream, const fpos_t *pos); +int fgetpos(FILE *stream, fpos_t *pos); +void *memset(void *ptr, int value, size_t num); + +//--------------------------------------------------------------------- + +int opener1(FILE *file) { + int rc; + fpos_t offset; + + if (file == nullptr ) { + return -1; + } + + rc = fgetpos(file, &offset); + if (rc != 0 ) { + return rc; + } + + /* Read in data from file */ + + rc = fsetpos(file, &offset); + if (rc != 0 ) { + return rc; + } + + return 0; +} + +//--------------------------------------------------------------------- + +int opener2(FILE *file) { + int rc; + fpos_t offset; + + memset(&offset, 0, sizeof(offset)); + + if (file == nullptr) { + return -1; + } + + /* Read in data from file */ + + rc = fsetpos(file, &offset); + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: file position indicator should be obtained only from 'fgetpos' [bugprone-fsetpos-argument-check] + if (rc != 0 ) { + return rc; + } + + return 0; +} + + +//---------------------------------------------------------------------