Skip to content

[clang] Prevent dangling StringRefs #98699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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
Loading