Skip to content

Commit

Permalink
[clang] Prevent dangling StringRefs (#98699)
Browse files Browse the repository at this point in the history
Summary:
Fix locations where dangling StringRefs are created.

* `ConstraintSatisfaction::SubstitutionDiagnostic`: typedef of
`std::pair<SourceLocation, StringRef>`

* `concepts::Requirement::SubstitutionDiagnostic`: struct whose 1st and
3rd data members are `StringRef`s

Fixes #98667

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251531
  • Loading branch information
JOE1994 authored and yuxuanchen1997 committed Jul 25, 2024
1 parent 3d7d691 commit 248f3de
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,8 @@ Bug Fixes in This Version
- ``__is_trivially_equality_comparable`` no longer returns true for types which
have a constrained defaulted comparison operator (#GH89293).

- Fixed Clang from generating dangling StringRefs when deserializing Exprs & Stmts (#GH98667)

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
18 changes: 15 additions & 3 deletions clang/lib/Serialization/ASTReaderStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,12 @@ void ASTStmtReader::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
E->setRParenLoc(readSourceLocation());
}

static StringRef saveStrToCtx(const std::string &S, ASTContext &Ctx) {
char *Buf = new (Ctx) char[S.size()];
std::copy(S.begin(), S.end(), Buf);
return StringRef(Buf, S.size());
}

static ConstraintSatisfaction
readConstraintSatisfaction(ASTRecordReader &Record) {
ConstraintSatisfaction Satisfaction;
Expand All @@ -795,7 +801,9 @@ readConstraintSatisfaction(ASTRecordReader &Record) {
for (unsigned i = 0; i != NumDetailRecords; ++i) {
if (/* IsDiagnostic */Record.readInt()) {
SourceLocation DiagLocation = Record.readSourceLocation();
std::string DiagMessage = Record.readString();
StringRef DiagMessage =
saveStrToCtx(Record.readString(), Record.getContext());

Satisfaction.Details.emplace_back(
new (Record.getContext())
ConstraintSatisfaction::SubstitutionDiagnostic(DiagLocation,
Expand All @@ -820,9 +828,13 @@ void ASTStmtReader::VisitConceptSpecializationExpr(

static concepts::Requirement::SubstitutionDiagnostic *
readSubstitutionDiagnostic(ASTRecordReader &Record) {
std::string SubstitutedEntity = Record.readString();
StringRef SubstitutedEntity =
saveStrToCtx(Record.readString(), Record.getContext());

SourceLocation DiagLoc = Record.readSourceLocation();
std::string DiagMessage = Record.readString();
StringRef DiagMessage =
saveStrToCtx(Record.readString(), Record.getContext());

return new (Record.getContext())
concepts::Requirement::SubstitutionDiagnostic{SubstitutedEntity, DiagLoc,
DiagMessage};
Expand Down

0 comments on commit 248f3de

Please sign in to comment.