[clang] [clang][UBSan] Fix null pointer dereference in unnamed tag methods in C mode (PR #215694)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 23 15:18:43 PDT 2026
https://github.com/Berazold updated https://github.com/llvm/llvm-project/pull/215694
>From 56d71afcb6e502fa7d6edd0f6b82c0e6f0983f61 Mon Sep 17 00:00:00 2001
From: iaroslav <yborobkin at gmail.com>
Date: Tue, 11 Aug 2026 23:25:19 +0100
Subject: [PATCH 1/4] Fix null pointer dereference in C mode
---
clang/lib/AST/ASTContext.cpp | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 47028c616e45c..0301cd87ff8e3 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -13848,21 +13848,23 @@ void ASTContext::addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
void ASTContext::addTypedefNameForUnnamedTagDecl(TagDecl *TD,
TypedefNameDecl *DD) {
- return ABI->addTypedefNameForUnnamedTagDecl(TD, DD);
+ if (ABI)
+ ABI->addTypedefNameForUnnamedTagDecl(TD, DD);
}
TypedefNameDecl *
ASTContext::getTypedefNameForUnnamedTagDecl(const TagDecl *TD) {
- return ABI->getTypedefNameForUnnamedTagDecl(TD);
+ return ABI ? ABI->getTypedefNameForUnnamedTagDecl(TD) : nullptr;
}
void ASTContext::addDeclaratorForUnnamedTagDecl(TagDecl *TD,
DeclaratorDecl *DD) {
- return ABI->addDeclaratorForUnnamedTagDecl(TD, DD);
+ if (ABI)
+ ABI->addDeclaratorForUnnamedTagDecl(TD, DD);
}
DeclaratorDecl *ASTContext::getDeclaratorForUnnamedTagDecl(const TagDecl *TD) {
- return ABI->getDeclaratorForUnnamedTagDecl(TD);
+ return ABI ? ABI->getDeclaratorForUnnamedTagDecl(TD) : nullptr;
}
void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) {
>From fb0d6c85bdfed424709999149d0b848b3aaf00cd Mon Sep 17 00:00:00 2001
From: iaroslav <yborobkin at gmail.com>
Date: Tue, 11 Aug 2026 23:33:33 +0100
Subject: [PATCH 2/4] Add test for anonymous struct function type hashing on
Windows
---
.../ubsan-function-anonymous-struct-windows.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
create mode 100644 clang/test/CodeGen/ubsan-function-anonymous-struct-windows.c
diff --git a/clang/test/CodeGen/ubsan-function-anonymous-struct-windows.c b/clang/test/CodeGen/ubsan-function-anonymous-struct-windows.c
new file mode 100644
index 0000000000000..d4a4fad2959d6
--- /dev/null
+++ b/clang/test/CodeGen/ubsan-function-anonymous-struct-windows.c
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -fsanitize=function -emit-llvm -o - %s | FileCheck %s
+
+// CHECK: define internal void @sample_tuple()
+// CHECK: !{{[0-9]+}} = !{i32 {{[0-9]+}}, !"typeid"}
+
+static struct { int first; float second; } sample_tuple(void) {
+ return (typeof(sample_tuple())){ .first = 10, .second = 0.1f };
+}
+
+void call_sample(void) {
+ sample_tuple();
+}
>From cbcbbe5a70f4470854a1941a24fd750af9575451 Mon Sep 17 00:00:00 2001
From: iaroslav <yborobkin at gmail.com>
Date: Wed, 19 Aug 2026 21:13:26 +0100
Subject: [PATCH 3/4] [Docs] Add release note for ASTContext unnamed tag crash
fix
---
clang/docs/ReleaseNotes.md | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index f719672e8a4e5..27ba4e3a656ae 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -388,6 +388,7 @@ features cannot lower the translation-unit ABI level;
- Fixed an assertion crash when instantiating a nested requirement with an invalid constraint. (#GH213575)
- Clang now defines the GCC-compatible predefined macro `__SIG_ATOMIC_TYPE__`. (#GH213895)
- Fixed a bug where a stray closing curley brace in an OpenMP/OpenACC pragma could cause pragma parsing issues when inside of a member function. (#GH214195)
+- Fixed a crash when compiling C code with `-fsanitize=function` targeting the MSVC ABI, where a function returns an anonymous struct. (#GH215689)
#### Bug Fixes to Compiler Builtins
>From 616b908c1244fcfa5922c2d5f38fc53be49286f0 Mon Sep 17 00:00:00 2001
From: iaroslav <yborobkin at gmail.com>
Date: Wed, 19 Aug 2026 21:48:34 +0100
Subject: [PATCH 4/4] [UBSan][Test] Fix anonymous struct function hashing test
---
clang/test/CodeGen/ubsan-function-anonymous-struct-windows.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/test/CodeGen/ubsan-function-anonymous-struct-windows.c b/clang/test/CodeGen/ubsan-function-anonymous-struct-windows.c
index d4a4fad2959d6..7ee3a680f3e0d 100644
--- a/clang/test/CodeGen/ubsan-function-anonymous-struct-windows.c
+++ b/clang/test/CodeGen/ubsan-function-anonymous-struct-windows.c
@@ -1,7 +1,6 @@
// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -fsanitize=function -emit-llvm -o - %s | FileCheck %s
-// CHECK: define internal void @sample_tuple()
-// CHECK: !{{[0-9]+}} = !{i32 {{[0-9]+}}, !"typeid"}
+// CHECK: define internal {{.*}} @sample_tuple(){{.*}} !func_sanitize !{{[0-9]+}}
static struct { int first; float second; } sample_tuple(void) {
return (typeof(sample_tuple())){ .first = 10, .second = 0.1f };
More information about the cfe-commits
mailing list