[clang-tools-extra] [clang-doc] Fix nullptr dereference in JSONGenerator::insertComment (PR #173336)

via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 22 22:40:06 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: Erick Velez (evelez7)

<details>
<summary>Changes</summary>

The `if` statements in `insertComment` didn't check whether or not the comment JSON object was a nullptr before dereferencing it. This crash only happened when running clang-doc on larger codebases, like `clang`.

---
Full diff: https://github.com/llvm/llvm-project/pull/173336.diff


1 Files Affected:

- (modified) clang-tools-extra/clang-doc/JSONGenerator.cpp (+3-2) 


``````````diff
diff --git a/clang-tools-extra/clang-doc/JSONGenerator.cpp b/clang-tools-extra/clang-doc/JSONGenerator.cpp
index 9c384ed7eeade..6dec347ed0bd0 100644
--- a/clang-tools-extra/clang-doc/JSONGenerator.cpp
+++ b/clang-tools-extra/clang-doc/JSONGenerator.cpp
@@ -98,11 +98,12 @@ static void insertComment(Object &Description, json::Value &Comment,
   // The comment has a Children array for the actual text, with meta attributes
   // alongside it in the Object.
   if (auto *Obj = Comment.getAsObject()) {
-    if (auto *Children = Obj->getArray("Children"); Children->empty())
+    if (auto *Children = Obj->getArray("Children");
+        Children && Children->empty())
       return;
   }
   // The comment is just an array of text comments.
-  else if (auto *Array = Comment.getAsArray(); Array->empty()) {
+  else if (auto *Array = Comment.getAsArray(); Array && Array->empty()) {
     return;
   }
 

``````````

</details>


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


More information about the cfe-commits mailing list