NotNullTerminatedResultCheck.h 2.9 KB

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