[clang] [llvm] [VFS] Guard against null key/value nodes when parsing YAML overlay (PR #190506)

via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 4 23:52:26 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Henry Jiang (mustartt)

<details>
<summary>Changes</summary>

When a VFS overlay YAML file contains malformed content such as tabs, the YAML parser can produce KeyValueNode entries where `getKey` returns nullptr. The VFS overlay parser then passes these null pointers to `parseScalarString`, which then calls dyn_cast on a nullptr.
  
Add null checks for `getKey` to bail out early on malformed input.


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


4 Files Affected:

- (added) clang/test/VFS/Inputs/invalid-key.yaml (+9) 
- (added) clang/test/VFS/Inputs/invalid-top-level-key.yaml (+5) 
- (modified) clang/test/VFS/parse-errors.c (+6) 
- (modified) llvm/lib/Support/VirtualFileSystem.cpp (+6) 


``````````diff
diff --git a/clang/test/VFS/Inputs/invalid-key.yaml b/clang/test/VFS/Inputs/invalid-key.yaml
new file mode 100644
index 0000000000000..3d873d1a163b2
--- /dev/null
+++ b/clang/test/VFS/Inputs/invalid-key.yaml
@@ -0,0 +1,9 @@
+# NOTE: This file contains an intentional tab character that triggers a YAML
+# parse failure. Do not replace tabs with spaces.
+version: 0
+redirecting-with: fallthrough
+roots:
+  - type: directory-remap
+	name: test
+    external-contents: test
+
diff --git a/clang/test/VFS/Inputs/invalid-top-level-key.yaml b/clang/test/VFS/Inputs/invalid-top-level-key.yaml
new file mode 100644
index 0000000000000..dd2071a9f6739
--- /dev/null
+++ b/clang/test/VFS/Inputs/invalid-top-level-key.yaml
@@ -0,0 +1,5 @@
+# NOTE: This file contains an intentional tab character that triggers a YAML
+# parse failure. Do not replace tabs with spaces.
+version: 0
+	redirecting-with: fallthrough
+roots: []
diff --git a/clang/test/VFS/parse-errors.c b/clang/test/VFS/parse-errors.c
index 8aa208438bcca..88f59a4c61067 100644
--- a/clang/test/VFS/parse-errors.c
+++ b/clang/test/VFS/parse-errors.c
@@ -20,3 +20,9 @@
 // RUN: not %clang_cc1 -ivfsoverlay %S/Inputs/redirect-and-fallthrough.yaml -fsyntax-only %s 2>&1 | FileCheck -check-prefix=CHECK-EXCLUSIVE-KEYS %s
 // CHECK-EXCLUSIVE-KEYS: 'fallthrough' and 'redirecting-with' are mutually exclusive
 // CHECK-EXCLUSIVE-KEYS: invalid virtual filesystem overlay file
+
+// RUN: not %clang_cc1 -ivfsoverlay %S/Inputs/invalid-key.yaml -fsyntax-only %s 2>&1 | FileCheck -check-prefix=CHECK-INVALID-KEY %s
+// CHECK-INVALID-KEY: invalid virtual filesystem overlay file
+
+// RUN: not %clang_cc1 -ivfsoverlay %S/Inputs/invalid-top-level-key.yaml -fsyntax-only %s 2>&1 | FileCheck -check-prefix=CHECK-INVALID-TOP %s
+// CHECK-INVALID-TOP: invalid virtual filesystem overlay file
diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp
index 8a7ca35a8dd25..2f1652318d329 100644
--- a/llvm/lib/Support/VirtualFileSystem.cpp
+++ b/llvm/lib/Support/VirtualFileSystem.cpp
@@ -1874,6 +1874,9 @@ class llvm::vfs::RedirectingFileSystemParser {
     RedirectingFileSystem::EntryKind Kind;
 
     for (auto &I : *M) {
+      if (!I.getKey() || !I.getValue())
+        return nullptr;
+
       StringRef Key;
       // Reuse the buffer for key and value, since we don't look at key after
       // parsing value.
@@ -2106,6 +2109,9 @@ class llvm::vfs::RedirectingFileSystemParser {
 
     // Parse configuration and 'roots'
     for (auto &I : *Top) {
+      if (!I.getKey() || !I.getValue())
+        return false;
+
       SmallString<10> KeyBuffer;
       StringRef Key;
       if (!parseScalarString(I.getKey(), Key, KeyBuffer))

``````````

</details>


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


More information about the cfe-commits mailing list