Skip to content

Commit

Permalink
Extract mem alloc & string copy logic to helper function
Browse files Browse the repository at this point in the history
Add Clang release note item
  • Loading branch information
JOE1994 committed Jul 13, 2024
1 parent 07648c7 commit 3b4dc1f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
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
28 changes: 15 additions & 13 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,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());
}
Expand All @@ -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) {
Expand Down

0 comments on commit 3b4dc1f

Please sign in to comment.