[clang] [clang] Prevent dangling StringRefs (PR #98699)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 12 15:59:08 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Youngsuk Kim (JOE1994)
<details>
<summary>Changes</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
---
Full diff: https://github.com/llvm/llvm-project/pull/98699.diff
1 Files Affected:
- (modified) clang/lib/Serialization/ASTReaderStmt.cpp (+16-5)
``````````diff
diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp
index 5a5d7be69a2c3..ecbc57d642ca7 100644
--- a/clang/lib/Serialization/ASTReaderStmt.cpp
+++ b/clang/lib/Serialization/ASTReaderStmt.cpp
@@ -797,10 +797,14 @@ readConstraintSatisfaction(ASTRecordReader &Record) {
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);
+
Satisfaction.Details.emplace_back(
- ConstraintExpr, new (Record.getContext())
- ConstraintSatisfaction::SubstitutionDiagnostic{
- DiagLocation, DiagMessage});
+ ConstraintExpr,
+ new (Record.getContext())
+ ConstraintSatisfaction::SubstitutionDiagnostic{
+ DiagLocation, StringRef(DBuf, DiagMessage.size())});
} else
Satisfaction.Details.emplace_back(ConstraintExpr, Record.readExpr());
}
@@ -822,11 +826,18 @@ 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);
+
SourceLocation DiagLoc = Record.readSourceLocation();
std::string DiagMessage = Record.readString();
+ char *DBuf = new (Record.getContext()) char[DiagMessage.size()];
+ std::copy(DiagMessage.begin(), DiagMessage.end(), DBuf);
+
return new (Record.getContext())
- concepts::Requirement::SubstitutionDiagnostic{SubstitutedEntity, DiagLoc,
- DiagMessage};
+ concepts::Requirement::SubstitutionDiagnostic{
+ StringRef(SBuf, SubstitutedEntity.size()), DiagLoc,
+ StringRef(DBuf, DiagMessage.size())};
}
void ASTStmtReader::VisitRequiresExpr(RequiresExpr *E) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/98699
More information about the cfe-commits
mailing list