Skip to content

Commit 248f3de

Browse files
JOE1994yuxuanchen1997
authored andcommitted
[clang] Prevent dangling StringRefs (#98699)
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
1 parent 3d7d691 commit 248f3de

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,8 @@ Bug Fixes in This Version
826826
- ``__is_trivially_equality_comparable`` no longer returns true for types which
827827
have a constrained defaulted comparison operator (#GH89293).
828828

829+
- Fixed Clang from generating dangling StringRefs when deserializing Exprs & Stmts (#GH98667)
830+
829831
Bug Fixes to Compiler Builtins
830832
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
831833

clang/lib/Serialization/ASTReaderStmt.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,12 @@ void ASTStmtReader::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
785785
E->setRParenLoc(readSourceLocation());
786786
}
787787

788+
static StringRef saveStrToCtx(const std::string &S, ASTContext &Ctx) {
789+
char *Buf = new (Ctx) char[S.size()];
790+
std::copy(S.begin(), S.end(), Buf);
791+
return StringRef(Buf, S.size());
792+
}
793+
788794
static ConstraintSatisfaction
789795
readConstraintSatisfaction(ASTRecordReader &Record) {
790796
ConstraintSatisfaction Satisfaction;
@@ -795,7 +801,9 @@ readConstraintSatisfaction(ASTRecordReader &Record) {
795801
for (unsigned i = 0; i != NumDetailRecords; ++i) {
796802
if (/* IsDiagnostic */Record.readInt()) {
797803
SourceLocation DiagLocation = Record.readSourceLocation();
798-
std::string DiagMessage = Record.readString();
804+
StringRef DiagMessage =
805+
saveStrToCtx(Record.readString(), Record.getContext());
806+
799807
Satisfaction.Details.emplace_back(
800808
new (Record.getContext())
801809
ConstraintSatisfaction::SubstitutionDiagnostic(DiagLocation,
@@ -820,9 +828,13 @@ void ASTStmtReader::VisitConceptSpecializationExpr(
820828

821829
static concepts::Requirement::SubstitutionDiagnostic *
822830
readSubstitutionDiagnostic(ASTRecordReader &Record) {
823-
std::string SubstitutedEntity = Record.readString();
831+
StringRef SubstitutedEntity =
832+
saveStrToCtx(Record.readString(), Record.getContext());
833+
824834
SourceLocation DiagLoc = Record.readSourceLocation();
825-
std::string DiagMessage = Record.readString();
835+
StringRef DiagMessage =
836+
saveStrToCtx(Record.readString(), Record.getContext());
837+
826838
return new (Record.getContext())
827839
concepts::Requirement::SubstitutionDiagnostic{SubstitutedEntity, DiagLoc,
828840
DiagMessage};

0 commit comments

Comments
 (0)