[clang] [clang-tools-extra] Fix OOM in FormatDiagnostic (PR #108187)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 12 06:14:24 PDT 2024


================
@@ -538,24 +511,51 @@ bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
     Emitted = (DiagLevel != DiagnosticIDs::Ignored);
     if (Emitted) {
       // Emit the diagnostic regardless of suppression level.
-      Diags->EmitDiag(*this, DiagLevel);
+      Diags->EmitDiag(*this, DB, DiagLevel);
     }
   } else {
     // Process the diagnostic, sending the accumulated information to the
     // DiagnosticConsumer.
-    Emitted = ProcessDiag();
+    Emitted = ProcessDiag(DB);
   }
 
-  // Clear out the current diagnostic object.
-  Clear();
+  return Emitted;
+}
 
-  // If there was a delayed diagnostic, emit it now.
-  if (!Force && DelayedDiagID)
-    ReportDelayed();
+DiagnosticBuilder::DiagnosticBuilder(DiagnosticsEngine *diagObj,
+                                     SourceLocation CurDiagLoc,
+                                     unsigned CurDiagID)
+    : StreamingDiagnostic(diagObj->DiagAllocator), DiagObj(diagObj),
+      CurDiagLoc(CurDiagLoc), CurDiagID(CurDiagID), IsActive(true) {
+  assert(diagObj && "DiagnosticBuilder requires a valid DiagnosticsEngine!");
+}
 
-  return Emitted;
+DiagnosticBuilder::DiagnosticBuilder(const DiagnosticBuilder &D)
+    : StreamingDiagnostic() {
+  CurDiagLoc = D.CurDiagLoc;
+  CurDiagID = D.CurDiagID;
+  FlagValue = D.FlagValue;
+  DiagObj = D.DiagObj;
+  DiagStorage = D.DiagStorage;
+  D.DiagStorage = nullptr;
+  Allocator = D.Allocator;
+  IsActive = D.IsActive;
+  IsForceEmit = D.IsForceEmit;
+  D.Clear();
 }
 
+Diagnostic::Diagnostic(const DiagnosticsEngine *DO,
+                       const DiagnosticBuilder &DiagBuilder)
+    : DiagObj(DO), CurDiagLoc(DiagBuilder.CurDiagLoc),
+      CurDiagID(DiagBuilder.CurDiagID), FlagValue(DiagBuilder.FlagValue),
+      DiagStorage(*DiagBuilder.getStorage()) {}
+
+Diagnostic::Diagnostic(const DiagnosticsEngine *DO, SourceLocation CurDiagLoc,
+                       unsigned CurDiagID, const DiagnosticStorage &DiagStorage,
+                       StringRef storedDiagMessage)
+    : DiagObj(DO), CurDiagLoc(CurDiagLoc), CurDiagID(CurDiagID),
+      DiagStorage(DiagStorage), StoredDiagMessage(storedDiagMessage) {}
----------------
AaronBallman wrote:

```suggestion
                       StringRef StoredDiagMessage)
    : DiagObj(DO), CurDiagLoc(CurDiagLoc), CurDiagID(CurDiagID),
      DiagStorage(DiagStorage), StoredDiagMessage(StoredDiagMessage) {}
```

https://github.com/llvm/llvm-project/pull/108187


More information about the cfe-commits mailing list