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

Henry Jiang via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 4 23:41:29 PDT 2026


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

None

>From 9738d4bbdae566e4780b17c842bd2b53c806ed2a Mon Sep 17 00:00:00 2001
From: Henry Jiang <henry_jiang2 at apple.com>
Date: Sat, 4 Apr 2026 23:32:06 -0700
Subject: [PATCH] Guard invalid yaml key

---
 clang/test/VFS/Inputs/invalid-key.yaml           |  9 +++++++++
 clang/test/VFS/Inputs/invalid-top-level-key.yaml |  5 +++++
 clang/test/VFS/Inputs/invalid-value.yaml         |  9 +++++++++
 clang/test/VFS/parse-errors.c                    | 10 ++++++++++
 llvm/lib/Support/VirtualFileSystem.cpp           | 10 ++++++++++
 5 files changed, 43 insertions(+)
 create mode 100644 clang/test/VFS/Inputs/invalid-key.yaml
 create mode 100644 clang/test/VFS/Inputs/invalid-top-level-key.yaml
 create mode 100644 clang/test/VFS/Inputs/invalid-value.yaml

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/Inputs/invalid-value.yaml b/clang/test/VFS/Inputs/invalid-value.yaml
new file mode 100644
index 0000000000000..49036e4dd8e30
--- /dev/null
+++ b/clang/test/VFS/Inputs/invalid-value.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/parse-errors.c b/clang/test/VFS/parse-errors.c
index 8aa208438bcca..3136867d09ee6 100644
--- a/clang/test/VFS/parse-errors.c
+++ b/clang/test/VFS/parse-errors.c
@@ -20,3 +20,13 @@
 // 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-value.yaml -fsyntax-only %s 2>&1 | FileCheck -check-prefix=CHECK-INVALID-VALUE %s
+// CHECK-INVALID-VALUE: expected string
+// CHECK-INVALID-VALUE: 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..aaa8c7a359a25 100644
--- a/llvm/lib/Support/VirtualFileSystem.cpp
+++ b/llvm/lib/Support/VirtualFileSystem.cpp
@@ -1874,6 +1874,11 @@ class llvm::vfs::RedirectingFileSystemParser {
     RedirectingFileSystem::EntryKind Kind;
 
     for (auto &I : *M) {
+      if (!I.getKey() || !I.getValue()) {
+        error(M, "invalid YAML in overlay mapping entry");
+        return nullptr;
+      }
+
       StringRef Key;
       // Reuse the buffer for key and value, since we don't look at key after
       // parsing value.
@@ -2106,6 +2111,11 @@ class llvm::vfs::RedirectingFileSystemParser {
 
     // Parse configuration and 'roots'
     for (auto &I : *Top) {
+      if (!I.getKey() || !I.getValue()) {
+        error(Top, "invalid YAML in overlay mapping entry");
+        return false;
+      }
+
       SmallString<10> KeyBuffer;
       StringRef Key;
       if (!parseScalarString(I.getKey(), Key, KeyBuffer))



More information about the llvm-commits mailing list