Skip to content

Commit aa8dda3

Browse files
committed
[clang] Diagnose dangling issues for cases where a gsl-pointer is
construct from a gsl-owner object in a container.
1 parent d94199c commit aa8dda3

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ Improvements to Clang's diagnostics
273273
- The lifetimebound and GSL analysis in clang are coherent, allowing clang to
274274
detect more use-after-free bugs. (#GH100549).
275275

276+
- Clang now diagnoses dangling cases where a gsl-pointer is constructed from a gsl-owner object inside a container (#GH100384).
277+
276278
Improvements to Clang's time-trace
277279
----------------------------------
278280

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,17 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
401401
visitLocalsRetainedByInitializer(Path, Arg, Visit, true);
402402
Path.pop_back();
403403
};
404-
auto VisitGSLPointerArg = [&](const Decl *D, Expr *Arg, bool Value) {
404+
auto VisitGSLPointerArg = [&](const FunctionDecl *Callee, Expr *Arg) {
405405
// We are not interested in the temporary base objects of gsl Pointers:
406406
// Temp().ptr; // Here ptr might not dangle.
407407
if (isa<MemberExpr>(Arg->IgnoreImpCasts()))
408408
return;
409-
// Once we initialized a value with a reference, it can no longer dangle.
410-
if (!Value) {
409+
auto ReturnType = Callee->getReturnType();
410+
411+
// Once we initialized a value with a non gsl-owner reference, it can no
412+
// longer dangle.
413+
if (ReturnType->isReferenceType() &&
414+
!isRecordWithAttr<OwnerAttr>(ReturnType->getPointeeType())) {
411415
for (const IndirectLocalPathEntry &PE : llvm::reverse(Path)) {
412416
if (PE.Kind == IndirectLocalPathEntry::GslReferenceInit ||
413417
PE.Kind == IndirectLocalPathEntry::LifetimeBoundCall)
@@ -418,9 +422,10 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
418422
break;
419423
}
420424
}
421-
Path.push_back({Value ? IndirectLocalPathEntry::GslPointerInit
422-
: IndirectLocalPathEntry::GslReferenceInit,
423-
Arg, D});
425+
Path.push_back({ReturnType->isReferenceType()
426+
? IndirectLocalPathEntry::GslReferenceInit
427+
: IndirectLocalPathEntry::GslPointerInit,
428+
Arg, Callee});
424429
if (Arg->isGLValue())
425430
visitLocalsRetainedByReferenceBinding(Path, Arg, RK_ReferenceBinding,
426431
Visit);
@@ -451,8 +456,7 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
451456
else if (EnableGSLAnalysis) {
452457
if (auto *CME = dyn_cast<CXXMethodDecl>(Callee);
453458
CME && shouldTrackImplicitObjectArg(CME))
454-
VisitGSLPointerArg(Callee, ObjectArg,
455-
!Callee->getReturnType()->isReferenceType());
459+
VisitGSLPointerArg(Callee, ObjectArg);
456460
}
457461
}
458462

@@ -463,13 +467,11 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
463467
VisitLifetimeBoundArg(Callee->getParamDecl(I), Args[I]);
464468
else if (EnableGSLAnalysis && I == 0) {
465469
if (shouldTrackFirstArgument(Callee)) {
466-
VisitGSLPointerArg(Callee, Args[0],
467-
!Callee->getReturnType()->isReferenceType());
470+
VisitGSLPointerArg(Callee, Args[0]);
468471
} else if (auto *CCE = dyn_cast<CXXConstructExpr>(Call);
469472
CCE &&
470473
CCE->getConstructor()->getParent()->hasAttr<PointerAttr>()) {
471-
VisitGSLPointerArg(CCE->getConstructor()->getParamDecl(0), Args[0],
472-
true);
474+
VisitGSLPointerArg(CCE->getConstructor(), Args[0]);
473475
}
474476
}
475477
}

clang/test/Sema/warn-lifetime-analysis-nocfg.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,34 @@ int &danglingRawPtrFromLocal3() {
275275
return *o; // expected-warning {{reference to stack memory associated with local variable 'o' returned}}
276276
}
277277

278+
// GH100384
279+
std::string_view containerWithAnnotatedElements() {
280+
std::string_view c1 = std::vector<std::string>().at(0); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}
281+
c1 = std::vector<std::string>().at(0); // expected-warning {{object backing the pointer}}
282+
283+
// no warning on constructing from gsl-pointer
284+
std::string_view c2 = std::vector<std::string_view>().at(0);
285+
286+
std::vector<std::string> local;
287+
return local.at(0); // expected-warning {{address of stack memory associated with local variable}}
288+
}
289+
290+
std::string_view localUniquePtr(int i) {
291+
std::unique_ptr<std::string> c1;
292+
if (i)
293+
return *c1; // expected-warning {{address of stack memory associated with local variable}}
294+
std::unique_ptr<std::string_view> c2;
295+
return *c2; // expect no-warning.
296+
}
297+
298+
std::string_view localOptional(int i) {
299+
std::optional<std::string> o;
300+
if (i)
301+
return o.value(); // expected-warning {{address of stack memory associated with local variable}}
302+
std::optional<std::string_view> abc;
303+
return abc.value(); // expect no warning
304+
}
305+
278306
const char *danglingRawPtrFromTemp() {
279307
return std::basic_string<char>().c_str(); // expected-warning {{returning address of local temporary object}}
280308
}

0 commit comments

Comments
 (0)