Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@ static unsigned getLength(const Expr *E,
if (!E)
return 0;

Expr::EvalResult Length;
E = E->IgnoreImpCasts();

if (const auto *LengthDRE = dyn_cast<DeclRefExpr>(E))
if (const auto *LengthVD = dyn_cast<VarDecl>(LengthDRE->getDecl()))
if (!isa<ParmVarDecl>(LengthVD))
if (const Expr *LengthInit = LengthVD->getInit())
if (LengthInit->EvaluateAsInt(Length, *Result.Context))
return Length.Val.getInt().getZExtValue();
if (!LengthInit->isValueDependent()) {
Comment on lines 72 to +73
Copy link
Contributor

@HerrCai0907 HerrCai0907 Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (const Expr *LengthInit = LengthVD->getInit(); LengthInit && !LengthInit->isValueDependent()) {

Expr::EvalResult Length;
if (LengthInit->EvaluateAsInt(Length, *Result.Context))
return Length.Val.getInt().getZExtValue();
}

if (const auto *LengthIL = dyn_cast<IntegerLiteral>(E))
return LengthIL->getValue().getZExtValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
// RUN: -- -std=c++17 -I %S/Inputs/not-null-terminated-result

// This test case reproduces the crash when the check tries to evaluate
// a value-dependent expression using EvaluateAsInt() in
// bugprone-not-null-terminated-result, where the src parameter of memcpy is
// value-dependent, but the length is not.

// expected-no-diagnostics

#include "not-null-terminated-result-cxx.h"

template<size_t N>
class ValueDependentClass {
public:
void copyData(char* Dst) {
const char* Src = reinterpret_cast<const char*>(this);
// The length parameter is arbitrary, but the crash is not reproduced if it is N.
memcpy(Dst, Src, 32);
}
};

template class ValueDependentClass<42>; // The template parameter value is arbitrary.
Loading