From 92a39aa4c8158270a67362944a826ba51287441d Mon Sep 17 00:00:00 2001 From: Evgeny Leviant Date: Thu, 9 Apr 2026 11:58:30 +0200 Subject: [PATCH] Don't omit null pointer checks with -fms-kernel --- clang/include/clang/Driver/Options.td | 4 ++-- clang/test/CodeGen/MSKernel/null-deref.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 clang/test/CodeGen/MSKernel/null-deref.c diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 4eb013d587eb..029e8fbf7c7c 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2732,10 +2732,10 @@ defm rewrite_includes : BoolFOption<"rewrite-includes", defm directives_only : OptInCC1FFlag<"directives-only", "">; defm delete_null_pointer_checks : BoolFOption<"delete-null-pointer-checks", - CodeGenOpts<"NullPointerIsValid">, DefaultFalse, + CodeGenOpts<"NullPointerIsValid">, Default<"LangOpts->Kernel">, NegFlag, - PosFlag, + PosFlag, BothFlags<[], [ClangOption, CLOption]>>, DocBrief<[{When enabled, treat null pointer dereference, creation of a reference to null, or passing a null pointer to a function parameter annotated with the "nonnull" diff --git a/clang/test/CodeGen/MSKernel/null-deref.c b/clang/test/CodeGen/MSKernel/null-deref.c new file mode 100644 index 000000000000..f23409115f50 --- /dev/null +++ b/clang/test/CodeGen/MSKernel/null-deref.c @@ -0,0 +1,16 @@ +// Check that null pointer checks are not omited in kernel mode compilations +// RUN: %clang_cc1 -fms-kernel -fms-extensions -triple x86_64-pc-windows-msvc %s -emit-llvm -o - | FileCheck %s +// RUN: %clang_cc1 -fms-kernel -fms-extensions -triple x86_64-pc-windows-msvc -fdelete-null-pointer-checks %s -emit-llvm -o - | FileCheck %s --check-prefix=NOCHECK + +// CHECK: define dso_local i32 @process(ptr noundef %p) #0 +// CHECK: attributes #0 = {{.*}} null_pointer_is_valid +// NOCHECK-NOT: null_pointer_is_valid + +struct Obj { int value; int extra; }; + +int process(struct Obj* p) { + int v = p->value; + if (!p) + return -1; + return v + p->extra; +}