diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 5dc0f8b7e0bbb8..055b426860df2b 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -830,6 +830,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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp index da67a4fcab5cb7..b2b0a30457d234 100644 --- a/clang/lib/Serialization/ASTReaderStmt.cpp +++ b/clang/lib/Serialization/ASTReaderStmt.cpp @@ -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; @@ -795,14 +801,13 @@ readConstraintSatisfaction(ASTRecordReader &Record) { for (unsigned i = 0; i != NumDetailRecords; ++i) { if (/* IsDiagnostic */Record.readInt()) { SourceLocation DiagLocation = Record.readSourceLocation(); - std::string DiagMessage = Record.readString(); - char *DBuf = new (Record.getContext()) char[DiagMessage.size()]; - std::copy(DiagMessage.begin(), DiagMessage.end(), DBuf); + StringRef DiagMessage = + saveStrToCtx(Record.readString(), Record.getContext()); Satisfaction.Details.emplace_back( new (Record.getContext()) ConstraintSatisfaction::SubstitutionDiagnostic(DiagLocation, - StringRef(DBuf, DiagMessage.size()))); + DiagMessage)); } else Satisfaction.Details.emplace_back(Record.readExpr()); } @@ -823,19 +828,16 @@ void ASTStmtReader::VisitConceptSpecializationExpr( static concepts::Requirement::SubstitutionDiagnostic * readSubstitutionDiagnostic(ASTRecordReader &Record) { - std::string SubstitutedEntity = Record.readString(); - char *SBuf = new (Record.getContext()) char[SubstitutedEntity.size()]; - std::copy(SubstitutedEntity.begin(), SubstitutedEntity.end(), SBuf); + StringRef SubstitutedEntity = + saveStrToCtx(Record.readString(), Record.getContext()); SourceLocation DiagLoc = Record.readSourceLocation(); - std::string DiagMessage = Record.readString(); - char *DBuf = new (Record.getContext()) char[DiagMessage.size()]; - std::copy(DiagMessage.begin(), DiagMessage.end(), DBuf); + StringRef DiagMessage = + saveStrToCtx(Record.readString(), Record.getContext()); return new (Record.getContext()) - concepts::Requirement::SubstitutionDiagnostic{ - StringRef(SBuf, SubstitutedEntity.size()), DiagLoc, - StringRef(DBuf, DiagMessage.size())}; + concepts::Requirement::SubstitutionDiagnostic{SubstitutedEntity, DiagLoc, + DiagMessage}; } void ASTStmtReader::VisitRequiresExpr(RequiresExpr *E) {