1 //===--- NotNullTerminatedResultCheck.h - clang-tidy ------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NOT_NULL_TERMINATED_RESULT_H 10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NOT_NULL_TERMINATED_RESULT_H 11 12 #include "../ClangTidyCheck.h" 13 14 namespace clang::tidy::bugprone { 15 16 /// Finds function calls where it is possible to cause a not null-terminated 17 /// result. Usually the proper length of a string is 'strlen(src) + 1' or 18 /// equal length of this expression, because the null terminator needs an extra 19 /// space. Without the null terminator it can result in undefined behaviour 20 /// when the string is read. 21 /// 22 /// For the user-facing documentation see: 23 /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/not-null-terminated-result.html 24 class NotNullTerminatedResultCheck : public ClangTidyCheck { 25 public: 26 NotNullTerminatedResultCheck(StringRef Name, ClangTidyContext *Context); 27 void storeOptions(ClangTidyOptions::OptionMap &Opts) override; 28 void registerMatchers(ast_matchers::MatchFinder *Finder) override; 29 void check(const ast_matchers::MatchFinder::MatchResult &Result) override; 30 void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, 31 Preprocessor *ModuleExpanderPP) override; 32 33 private: 34 // If non-zero it is specifying if the target environment is considered to 35 // implement '_s' suffixed memory and string handler functions which are safer 36 // than older version (e.g. 'memcpy_s()'). The default value is '1'. 37 const bool WantToUseSafeFunctions; 38 39 bool UseSafeFunctions = false; 40 41 void memoryHandlerFunctionFix( 42 StringRef Name, const ast_matchers::MatchFinder::MatchResult &Result); 43 void memcpyFix(StringRef Name, 44 const ast_matchers::MatchFinder::MatchResult &Result, 45 DiagnosticBuilder &Diag); 46 void memcpy_sFix(StringRef Name, 47 const ast_matchers::MatchFinder::MatchResult &Result, 48 DiagnosticBuilder &Diag); 49 void memchrFix(StringRef Name, 50 const ast_matchers::MatchFinder::MatchResult &Result); 51 void memmoveFix(StringRef Name, 52 const ast_matchers::MatchFinder::MatchResult &Result, 53 DiagnosticBuilder &Diag) const; 54 void strerror_sFix(const ast_matchers::MatchFinder::MatchResult &Result); 55 void ncmpFix(StringRef Name, 56 const ast_matchers::MatchFinder::MatchResult &Result); 57 void xfrmFix(StringRef Name, 58 const ast_matchers::MatchFinder::MatchResult &Result); 59 }; 60 61 } // namespace clang::tidy::bugprone 62 63 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NOT_NULL_TERMINATED_RESULT_H 64