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 {
15 namespace tidy {
16 namespace bugprone {
17 
18 /// Finds function calls where it is possible to cause a not null-terminated
19 /// result. Usually the proper length of a string is 'strlen(src) + 1' or
20 /// equal length of this expression, because the null terminator needs an extra
21 /// space. Without the null terminator it can result in undefined behaviour
22 /// when the string is read.
23 ///
24 /// For the user-facing documentation see:
25 /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-not-null-terminated-result.html
26 class NotNullTerminatedResultCheck : public ClangTidyCheck {
27 public:
28   NotNullTerminatedResultCheck(StringRef Name, ClangTidyContext *Context);
29   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
30   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
31   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
32   void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
33                            Preprocessor *ModuleExpanderPP) override;
34 
35 private:
36   // If non-zero it is specifying if the target environment is considered to
37   // implement '_s' suffixed memory and string handler functions which are safer
38   // than older version (e.g. 'memcpy_s()'). The default value is '1'.
39   const bool WantToUseSafeFunctions;
40 
41   bool UseSafeFunctions = false;
42 
43   void memoryHandlerFunctionFix(
44       StringRef Name, const ast_matchers::MatchFinder::MatchResult &Result);
45   void memcpyFix(StringRef Name,
46                  const ast_matchers::MatchFinder::MatchResult &Result,
47                  DiagnosticBuilder &Diag);
48   void memcpy_sFix(StringRef Name,
49                    const ast_matchers::MatchFinder::MatchResult &Result,
50                    DiagnosticBuilder &Diag);
51   void memchrFix(StringRef Name,
52                  const ast_matchers::MatchFinder::MatchResult &Result);
53   void memmoveFix(StringRef Name,
54                   const ast_matchers::MatchFinder::MatchResult &Result,
55                   DiagnosticBuilder &Diag);
56   void strerror_sFix(const ast_matchers::MatchFinder::MatchResult &Result);
57   void ncmpFix(StringRef Name,
58                const ast_matchers::MatchFinder::MatchResult &Result);
59   void xfrmFix(StringRef Name,
60                const ast_matchers::MatchFinder::MatchResult &Result);
61 };
62 
63 } // namespace bugprone
64 } // namespace tidy
65 } // namespace clang
66 
67 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NOT_NULL_TERMINATED_RESULT_H
68