[clang] [clang][Lex] Add null check for `IncludeTok` in `PreprocessingRecord::InclusionDirective` (PR #192051)

via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 14 06:01:56 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: 🍌Shawn (StepfenShawn)

<details>
<summary>Changes</summary>

Add a defensive null check for `getIdentifierInfo()` in `PreprocessingRecord::InclusionDirective` to prevent potential null pointer dereference.
The function assumes the token has valid identifier info, but `getIdentifierInfo()` can return nullptr for literals or EOF tokens.

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


1 Files Affected:

- (modified) clang/lib/Lex/PreprocessingRecord.cpp (+5-1) 


``````````diff
diff --git a/clang/lib/Lex/PreprocessingRecord.cpp b/clang/lib/Lex/PreprocessingRecord.cpp
index f76155b1c45d6..d0f09552a003b 100644
--- a/clang/lib/Lex/PreprocessingRecord.cpp
+++ b/clang/lib/Lex/PreprocessingRecord.cpp
@@ -473,7 +473,11 @@ void PreprocessingRecord::InclusionDirective(
     bool ModuleImported, SrcMgr::CharacteristicKind FileType) {
   InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
 
-  switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
+  IdentifierInfo *II = IncludeTok.getIdentifierInfo();
+  if (!II)
+    llvm_unreachable("Invalid include directive token");
+
+  switch (II->getPPKeywordID()) {
   case tok::pp_include:
     Kind = InclusionDirective::Include;
     break;

``````````

</details>


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


More information about the cfe-commits mailing list