[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
Sun Apr 5 12:58:21 PDT 2026
https://github.com/mustartt updated https://github.com/llvm/llvm-project/pull/190506
>From 377824ee8b5e97ef2ddd2c2653165e49357ea141 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 1/2] 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/parse-errors.c | 6 ++++++
llvm/lib/Support/VirtualFileSystem.cpp | 6 ++++++
4 files changed, 26 insertions(+)
create mode 100644 clang/test/VFS/Inputs/invalid-key.yaml
create mode 100644 clang/test/VFS/Inputs/invalid-top-level-key.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/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))
>From ceff4f2640b464fe8d6204691882e2c4c60f0b53 Mon Sep 17 00:00:00 2001
From: Henry Jiang <henry_jiang2 at apple.com>
Date: Sun, 5 Apr 2026 12:58:02 -0700
Subject: [PATCH 2/2] update check
---
llvm/lib/Support/VirtualFileSystem.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp
index 2f1652318d329..da9743184f7c3 100644
--- a/llvm/lib/Support/VirtualFileSystem.cpp
+++ b/llvm/lib/Support/VirtualFileSystem.cpp
@@ -1874,7 +1874,7 @@ class llvm::vfs::RedirectingFileSystemParser {
RedirectingFileSystem::EntryKind Kind;
for (auto &I : *M) {
- if (!I.getKey() || !I.getValue())
+ if (!I.getKey())
return nullptr;
StringRef Key;
@@ -2109,7 +2109,7 @@ class llvm::vfs::RedirectingFileSystemParser {
// Parse configuration and 'roots'
for (auto &I : *Top) {
- if (!I.getKey() || !I.getValue())
+ if (!I.getKey())
return false;
SmallString<10> KeyBuffer;
More information about the llvm-commits
mailing list