File tree Expand file tree Collapse file tree 3 files changed +31
-2
lines changed Expand file tree Collapse file tree 3 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -64,15 +64,17 @@ static unsigned getLength(const Expr *E,
6464 if (!E)
6565 return 0 ;
6666
67- Expr::EvalResult Length;
6867 E = E->IgnoreImpCasts ();
6968
7069 if (const auto *LengthDRE = dyn_cast<DeclRefExpr>(E))
7170 if (const auto *LengthVD = dyn_cast<VarDecl>(LengthDRE->getDecl ()))
7271 if (!isa<ParmVarDecl>(LengthVD))
73- if (const Expr *LengthInit = LengthVD->getInit ())
72+ if (const Expr *LengthInit = LengthVD->getInit ();
73+ LengthInit && !LengthInit->isValueDependent ()) {
74+ Expr::EvalResult Length;
7475 if (LengthInit->EvaluateAsInt (Length, *Result.Context ))
7576 return Length.Val .getInt ().getZExtValue ();
77+ }
7678
7779 if (const auto *LengthIL = dyn_cast<IntegerLiteral>(E))
7880 return LengthIL->getValue ().getZExtValue ();
Original file line number Diff line number Diff line change @@ -274,6 +274,10 @@ Changes in existing checks
274274 <clang-tidy/checks/bugprone/narrowing-conversions>` check by fixing
275275 false positive from analysis of a conditional expression in C.
276276
277+ - Improved :doc: `bugprone-not-null-terminated-result
278+ <clang-tidy/checks/bugprone/not-null-terminated-result>` check by fixing
279+ a crash caused by certain value-dependent expressions.
280+
277281- Improved :doc: `bugprone-reserved-identifier
278282 <clang-tidy/checks/bugprone/reserved-identifier>` check by ignoring
279283 declarations and macros in system headers.
Original file line number Diff line number Diff line change 1+ // RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
2+ // RUN: -- -std=c++17 -I %S/Inputs/not-null-terminated-result
3+
4+ // This test case reproduces the crash when the check tries to evaluate
5+ // a value-dependent expression using EvaluateAsInt() in
6+ // bugprone-not-null-terminated-result, where the src parameter of memcpy is
7+ // value-dependent, but the length is not.
8+
9+ // expected-no-diagnostics
10+
11+ #include " not-null-terminated-result-cxx.h"
12+
13+ template <size_t N>
14+ class ValueDependentClass {
15+ public:
16+ void copyData (char * Dst) {
17+ const char * Src = reinterpret_cast <const char *>(this );
18+ // The length parameter is arbitrary, but the crash is not reproduced if it is N.
19+ memcpy (Dst, Src, 32 );
20+ }
21+ };
22+
23+ template class ValueDependentClass <42 >; // The template parameter value is arbitrary.
You can’t perform that action at this time.
0 commit comments