Skip to content

Commit f8514f0

Browse files
nectotstellar
authored andcommitted
Fix assertion failure "PathDiagnosticSpotPiece's must have a valid location." in ReturnPtrRange checker on builtin functions
Builtin functions (such as `std::move`, `std::forward`, `std::as_const`) have a body generated during the analysis not related to any source file so their statements have no valid source locations. `ReturnPtrRange` checker should not report issues for these builtin functions because they only forward its parameter and do not create any new pointers. Fixes #55347 Patch by Arseniy Zaostrovnykh. Reviewed By: NoQ Differential Revision: https://reviews.llvm.org/D138713 (cherry picked from commit 98d5509)
1 parent 3bb5ebc commit f8514f0

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

clang/lib/StaticAnalyzer/Checkers/ReturnPointerRangeChecker.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ void ReturnPointerRangeChecker::checkPreStmt(const ReturnStmt *RS,
4141
if (!RetE)
4242
return;
4343

44+
// Skip "body farmed" functions.
45+
if (RetE->getSourceRange().isInvalid())
46+
return;
47+
4448
SVal V = C.getSVal(RetE);
4549
const MemRegion *R = V.getAsRegion();
4650

clang/test/Analysis/return-ptr-range.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,14 @@ Data *test_struct_array() {
115115

116116
}
117117

118+
namespace std {
119+
// A builtin function with the body generated on the fly.
120+
template <typename T> T&& move(T &&) noexcept;
121+
} // namespace std
122+
123+
char buf[2];
124+
125+
void top() {
126+
// see https://github.com/llvm/llvm-project/issues/55347
127+
(void)std::move(*(buf + 3)); // no-crash
128+
}

0 commit comments

Comments
 (0)