Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:19

0001 //===--- NotNullTerminatedResultCheck.h - clang-tidy ------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NOT_NULL_TERMINATED_RESULT_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NOT_NULL_TERMINATED_RESULT_H
0011 
0012 #include "../ClangTidyCheck.h"
0013 
0014 namespace clang::tidy::bugprone {
0015 
0016 /// Finds function calls where it is possible to cause a not null-terminated
0017 /// result. Usually the proper length of a string is 'strlen(src) + 1' or
0018 /// equal length of this expression, because the null terminator needs an extra
0019 /// space. Without the null terminator it can result in undefined behaviour
0020 /// when the string is read.
0021 ///
0022 /// For the user-facing documentation see:
0023 /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/not-null-terminated-result.html
0024 class NotNullTerminatedResultCheck : public ClangTidyCheck {
0025 public:
0026   NotNullTerminatedResultCheck(StringRef Name, ClangTidyContext *Context);
0027   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
0028   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
0029   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
0030   void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
0031                            Preprocessor *ModuleExpanderPP) override;
0032 
0033 private:
0034   // If non-zero it is specifying if the target environment is considered to
0035   // implement '_s' suffixed memory and string handler functions which are safer
0036   // than older version (e.g. 'memcpy_s()'). The default value is '1'.
0037   const bool WantToUseSafeFunctions;
0038 
0039   bool UseSafeFunctions = false;
0040 
0041   void memoryHandlerFunctionFix(
0042       StringRef Name, const ast_matchers::MatchFinder::MatchResult &Result);
0043   void memcpyFix(StringRef Name,
0044                  const ast_matchers::MatchFinder::MatchResult &Result,
0045                  DiagnosticBuilder &Diag);
0046   void memcpy_sFix(StringRef Name,
0047                    const ast_matchers::MatchFinder::MatchResult &Result,
0048                    DiagnosticBuilder &Diag);
0049   void memchrFix(StringRef Name,
0050                  const ast_matchers::MatchFinder::MatchResult &Result);
0051   void memmoveFix(StringRef Name,
0052                   const ast_matchers::MatchFinder::MatchResult &Result,
0053                   DiagnosticBuilder &Diag) const;
0054   void strerror_sFix(const ast_matchers::MatchFinder::MatchResult &Result);
0055   void ncmpFix(StringRef Name,
0056                const ast_matchers::MatchFinder::MatchResult &Result);
0057   void xfrmFix(StringRef Name,
0058                const ast_matchers::MatchFinder::MatchResult &Result);
0059 };
0060 
0061 } // namespace clang::tidy::bugprone
0062 
0063 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NOT_NULL_TERMINATED_RESULT_H