SuspiciousMemoryComparisonCheck.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //===--- SuspiciousMemoryComparisonCheck.cpp - clang-tidy -----------------===//
  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. #include "SuspiciousMemoryComparisonCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. #include <optional>
  12. using namespace clang::ast_matchers;
  13. namespace clang::tidy::bugprone {
  14. static std::optional<uint64_t> tryEvaluateSizeExpr(const Expr *SizeExpr,
  15. const ASTContext &Ctx) {
  16. Expr::EvalResult Result;
  17. if (SizeExpr->EvaluateAsRValue(Result, Ctx))
  18. return Ctx.toBits(
  19. CharUnits::fromQuantity(Result.Val.getInt().getExtValue()));
  20. return std::nullopt;
  21. }
  22. void SuspiciousMemoryComparisonCheck::registerMatchers(MatchFinder *Finder) {
  23. Finder->addMatcher(
  24. callExpr(allOf(callee(namedDecl(
  25. anyOf(hasName("::memcmp"), hasName("::std::memcmp")))),
  26. unless(isInstantiationDependent())))
  27. .bind("call"),
  28. this);
  29. }
  30. void SuspiciousMemoryComparisonCheck::check(
  31. const MatchFinder::MatchResult &Result) {
  32. const ASTContext &Ctx = *Result.Context;
  33. const auto *CE = Result.Nodes.getNodeAs<CallExpr>("call");
  34. const Expr *SizeExpr = CE->getArg(2);
  35. assert(SizeExpr != nullptr && "Third argument of memcmp is mandatory.");
  36. std::optional<uint64_t> ComparedBits = tryEvaluateSizeExpr(SizeExpr, Ctx);
  37. for (unsigned int ArgIndex = 0; ArgIndex < 2; ++ArgIndex) {
  38. const Expr *ArgExpr = CE->getArg(ArgIndex);
  39. QualType ArgType = ArgExpr->IgnoreImplicit()->getType();
  40. const Type *PointeeType = ArgType->getPointeeOrArrayElementType();
  41. assert(PointeeType != nullptr && "PointeeType should always be available.");
  42. QualType PointeeQualifiedType(PointeeType, 0);
  43. if (PointeeType->isRecordType()) {
  44. if (const RecordDecl *RD =
  45. PointeeType->getAsRecordDecl()->getDefinition()) {
  46. if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
  47. if (!CXXDecl->isStandardLayout()) {
  48. diag(CE->getBeginLoc(),
  49. "comparing object representation of non-standard-layout type "
  50. "%0; consider using a comparison operator instead")
  51. << PointeeQualifiedType;
  52. break;
  53. }
  54. }
  55. }
  56. }
  57. if (!PointeeType->isIncompleteType()) {
  58. uint64_t PointeeSize = Ctx.getTypeSize(PointeeType);
  59. if (ComparedBits && *ComparedBits >= PointeeSize &&
  60. !Ctx.hasUniqueObjectRepresentations(PointeeQualifiedType)) {
  61. diag(CE->getBeginLoc(),
  62. "comparing object representation of type %0 which does not have a "
  63. "unique object representation; consider comparing %select{the "
  64. "values|the members of the object}1 manually")
  65. << PointeeQualifiedType << (PointeeType->isRecordType() ? 1 : 0);
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. } // namespace clang::tidy::bugprone