https://github.com/JOE1994 created 
https://github.com/llvm/llvm-project/pull/98699

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

>From 68d85e0409c2222e6a3184f53918b99b3e77bfab Mon Sep 17 00:00:00 2001
From: Youngsuk Kim <youngsuk....@hpe.com>
Date: Fri, 12 Jul 2024 17:40:59 -0500
Subject: [PATCH] [clang] Prevent dangling StringRefs

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
---
 clang/lib/Serialization/ASTReaderStmt.cpp | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

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) {

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to