[clang] [clang] Fix crashes when initializing constexpr int* with floating-point (PR #180376)
Samrudh Nelli via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 8 11:17:02 PST 2026
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 <samrudhnelli at gmail.com>
Date: Sun, 8 Feb 2026 04:33:08 +0530
Subject: [PATCH 1/2] [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 <samrudhnelli at gmail.com>
Date: Mon, 9 Feb 2026 00:36:27 +0530
Subject: [PATCH 2/2] 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
More information about the cfe-commits
mailing list