https://github.com/SamrudhNelli updated 
https://github.com/llvm/llvm-project/pull/180376

>From f26fdaae133bb87e90c902706938ef87511edfcd Mon Sep 17 00:00:00 2001
From: Samrudh Nelli <[email protected]>
Date: Sun, 8 Feb 2026 04:33:08 +0530
Subject: [PATCH 1/3] [clang] Fix crashes when initializing constexpr int* with
 floating-point

Call isNullPointer() only when we are sure that the Rvalue is a pointer.
---
 clang/lib/Sema/SemaInit.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index ff278bc7471bd..3c19512595bae 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -8503,8 +8503,9 @@ ExprResult InitializationSequence::Perform(Sema &S,
         Expr::EvalResult ER;
         if (Entity.getType()->getAs<PointerType>() &&
             CurInit.get()->EvaluateAsRValue(ER, S.Context) &&
-            !ER.Val.isNullPointer()) {
+            (!ER.Val.isLValue() || !ER.Val.isNullPointer())) {
           S.Diag(Kind.getLocation(), diag::err_c23_constexpr_pointer_not_null);
+          return ExprError();
         }
       }
 

>From c443fe92359fdc2f6f44ee66aefe06c1e71a8a84 Mon Sep 17 00:00:00 2001
From: Samrudh Nelli <[email protected]>
Date: Mon, 9 Feb 2026 00:36:27 +0530
Subject: [PATCH 2/3] fix: update the if statement to print just one
 self-explanatory error

add the fix in ReleaseNotes
add a test for future builds
---
 clang/docs/ReleaseNotes.rst                     | 1 +
 clang/lib/Sema/SemaInit.cpp                     | 3 ++-
 clang/test/Sema/init-constexpr-ptr-with-float.c | 5 +++++
 3 files changed, 8 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Sema/init-constexpr-ptr-with-float.c

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0dbea8efc2642..6c55fcd8a9038 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -274,6 +274,7 @@ Miscellaneous Clang Crashes Fixed
 - Fixed a crash when using loop hint with a value dependent argument inside a
   generic lambda. (#GH172289)
 - Fixed a crash in C++ overload resolution with ``_Atomic``-qualified argument 
types. (#GH170433)
+- Fixed a crash when initializing a ``constexpr`` pointer with a 
floating-point literal. (#GH180313)
 
 OpenACC Specific Changes
 ------------------------
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 3c19512595bae..1818b55e41399 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -8503,7 +8503,8 @@ ExprResult InitializationSequence::Perform(Sema &S,
         Expr::EvalResult ER;
         if (Entity.getType()->getAs<PointerType>() &&
             CurInit.get()->EvaluateAsRValue(ER, S.Context) &&
-            (!ER.Val.isLValue() || !ER.Val.isNullPointer())) {
+            // We continue and let it error later if Rvalue is not a pointer
+            ER.Val.isLValue() && !ER.Val.isNullPointer()) {
           S.Diag(Kind.getLocation(), diag::err_c23_constexpr_pointer_not_null);
           return ExprError();
         }
diff --git a/clang/test/Sema/init-constexpr-ptr-with-float.c 
b/clang/test/Sema/init-constexpr-ptr-with-float.c
new file mode 100644
index 0000000000000..a90c422951f89
--- /dev/null
+++ b/clang/test/Sema/init-constexpr-ptr-with-float.c
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -std=c23 -fsyntax-only -verify %s
+
+// PR180313: Fix crashes when initializing constexpr int* with a 
floating-point value
+
+constexpr int *p = 0.0; // expected-error {{initializing 'int *const' with an 
expression of incompatible type 'double'}}
\ No newline at end of file

>From 0cb79b38d8b0b97e0ce5d1f4378511a365bf0761 Mon Sep 17 00:00:00 2001
From: Samrudh Nelli <[email protected]>
Date: Mon, 9 Feb 2026 12:50:23 +0530
Subject: [PATCH 3/3] fix: update the outdated clang tests

---
 clang/lib/Sema/SemaInit.cpp         | 4 +++-
 clang/test/AST/ByteCode/constexpr.c | 1 -
 clang/test/Sema/constexpr.c         | 1 -
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 1818b55e41399..f0d3d29470b1b 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -8504,7 +8504,9 @@ ExprResult InitializationSequence::Perform(Sema &S,
         if (Entity.getType()->getAs<PointerType>() &&
             CurInit.get()->EvaluateAsRValue(ER, S.Context) &&
             // We continue and let it error later if Rvalue is not a pointer
-            ER.Val.isLValue() && !ER.Val.isNullPointer()) {
+            ER.Val.isLValue() && 
+            !ER.Val.isNullPointer() &&
+            !ER.Val.getLValueBase().isNull()) {
           S.Diag(Kind.getLocation(), diag::err_c23_constexpr_pointer_not_null);
           return ExprError();
         }
diff --git a/clang/test/AST/ByteCode/constexpr.c 
b/clang/test/AST/ByteCode/constexpr.c
index af96bf3a06f37..68a433984f4c4 100644
--- a/clang/test/AST/ByteCode/constexpr.c
+++ b/clang/test/AST/ByteCode/constexpr.c
@@ -311,7 +311,6 @@ constexpr int *V83 = V82;
 constexpr int *V84 = 42;
 // both-error@-1 {{constexpr variable 'V84' must be initialized by a constant 
expression}}
 // both-note@-2 {{this conversion is not allowed in a constant expression}}
-// both-error@-3 {{constexpr pointer initializer is not null}}
 constexpr int *V85 = nullptr;
 
 // Check that constexpr variables should not be VLAs.
diff --git a/clang/test/Sema/constexpr.c b/clang/test/Sema/constexpr.c
index ae01c71e09b06..a6239e1cd5dd5 100644
--- a/clang/test/Sema/constexpr.c
+++ b/clang/test/Sema/constexpr.c
@@ -311,7 +311,6 @@ constexpr int *V83 = V82;
 constexpr int *V84 = 42;
 // expected-error@-1 {{constexpr variable 'V84' must be initialized by a 
constant expression}}
 // expected-note@-2 {{this conversion is not allowed in a constant expression}}
-// expected-error@-3 {{constexpr pointer initializer is not null}}
 constexpr int *V85 = nullptr;
 
 // Check that constexpr variables should not be VLAs.

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to