[clang] [clang] Fix crashes when initializing constexpr int* with floating-point (PR #180376)
Samrudh Nelli via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 16 05:14:54 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/7] [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/7] 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 <samrudhnelli at gmail.com>
Date: Mon, 9 Feb 2026 12:50:23 +0530
Subject: [PATCH 3/7] 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 at -1 {{constexpr variable 'V84' must be initialized by a constant expression}}
// both-note at -2 {{this conversion is not allowed in a constant expression}}
-// both-error at -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 at -1 {{constexpr variable 'V84' must be initialized by a constant expression}}
// expected-note at -2 {{this conversion is not allowed in a constant expression}}
-// expected-error at -3 {{constexpr pointer initializer is not null}}
constexpr int *V85 = nullptr;
// Check that constexpr variables should not be VLAs.
>From cb3df8484faf1198b5267b5bbf4c3fc959c62006 Mon Sep 17 00:00:00 2001
From: Samrudh Nelli <samrudhnelli at gmail.com>
Date: Mon, 9 Feb 2026 20:20:20 +0530
Subject: [PATCH 4/7] fix: generalize the error message "constexpr pointer
initializer is not null"
---
clang/lib/Sema/SemaInit.cpp | 6 ++----
clang/test/AST/ByteCode/constexpr.c | 5 +++--
clang/test/Sema/constexpr.c | 5 +++--
clang/test/Sema/init-constexpr-ptr-with-float.c | 5 -----
4 files changed, 8 insertions(+), 13 deletions(-)
delete mode 100644 clang/test/Sema/init-constexpr-ptr-with-float.c
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index f0d3d29470b1b..3774c0fb6fef3 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -8503,10 +8503,8 @@ ExprResult InitializationSequence::Perform(Sema &S,
Expr::EvalResult ER;
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.getLValueBase().isNull()) {
+ // Error if the result is not a pointer or not a null 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/AST/ByteCode/constexpr.c b/clang/test/AST/ByteCode/constexpr.c
index 68a433984f4c4..d17611e43744b 100644
--- a/clang/test/AST/ByteCode/constexpr.c
+++ b/clang/test/AST/ByteCode/constexpr.c
@@ -309,9 +309,10 @@ constexpr const int *V81 = &V80;
constexpr int *V82 = 0;
constexpr int *V83 = V82;
constexpr int *V84 = 42;
-// both-error at -1 {{constexpr variable 'V84' must be initialized by a constant expression}}
-// both-note at -2 {{this conversion is not allowed in a constant expression}}
+// both-error at -1 {{constexpr pointer initializer is not null}}
constexpr int *V85 = nullptr;
+constexpr int *V91 = 0.;
+// both-error at -1 {{constexpr pointer initializer is not null}}
// Check that constexpr variables should not be VLAs.
void f6(const int P1) {
diff --git a/clang/test/Sema/constexpr.c b/clang/test/Sema/constexpr.c
index a6239e1cd5dd5..b3ab9b91d11c2 100644
--- a/clang/test/Sema/constexpr.c
+++ b/clang/test/Sema/constexpr.c
@@ -309,9 +309,10 @@ constexpr const int *V81 = &V80;
constexpr int *V82 = 0;
constexpr int *V83 = V82;
constexpr int *V84 = 42;
-// expected-error at -1 {{constexpr variable 'V84' must be initialized by a constant expression}}
-// expected-note at -2 {{this conversion is not allowed in a constant expression}}
+// expected-error at -1 {{constexpr pointer initializer is not null}}
constexpr int *V85 = nullptr;
+constexpr int *V91 = 0.0;
+// expected-error at -1 {{constexpr pointer initializer is not null}}
// Check that constexpr variables should not be VLAs.
void f6(const int P1) {
diff --git a/clang/test/Sema/init-constexpr-ptr-with-float.c b/clang/test/Sema/init-constexpr-ptr-with-float.c
deleted file mode 100644
index a90c422951f89..0000000000000
--- a/clang/test/Sema/init-constexpr-ptr-with-float.c
+++ /dev/null
@@ -1,5 +0,0 @@
-// 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 90fd37cd3fa01135b3eeee9a1d62a5ac955a0ceb Mon Sep 17 00:00:00 2001
From: Samrudh Nelli <samrudhnelli at gmail.com>
Date: Wed, 11 Feb 2026 02:40:58 +0530
Subject: [PATCH 5/7] fix: revert to incompatible error for incompatible types
---
clang/lib/Sema/SemaInit.cpp | 2 +-
clang/test/AST/ByteCode/constexpr.c | 2 +-
clang/test/Sema/constexpr.c | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 3774c0fb6fef3..9b6162e51a0ac 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -8504,7 +8504,7 @@ ExprResult InitializationSequence::Perform(Sema &S,
if (Entity.getType()->getAs<PointerType>() &&
CurInit.get()->EvaluateAsRValue(ER, S.Context) &&
// Error if the result is not a pointer or not a null pointer
- (!ER.Val.isLValue() || !ER.Val.isNullPointer())) {
+ (ER.Val.isLValue() && !ER.Val.isNullPointer())) {
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 d17611e43744b..dfe3d667f27cc 100644
--- a/clang/test/AST/ByteCode/constexpr.c
+++ b/clang/test/AST/ByteCode/constexpr.c
@@ -312,7 +312,7 @@ constexpr int *V84 = 42;
// both-error at -1 {{constexpr pointer initializer is not null}}
constexpr int *V85 = nullptr;
constexpr int *V91 = 0.;
-// both-error at -1 {{constexpr pointer initializer is not null}}
+// both-error at -1 {{initializing 'int *const' with an expression of incompatible type 'double'}}
// Check that constexpr variables should not be VLAs.
void f6(const int P1) {
diff --git a/clang/test/Sema/constexpr.c b/clang/test/Sema/constexpr.c
index b3ab9b91d11c2..04da0f56a741d 100644
--- a/clang/test/Sema/constexpr.c
+++ b/clang/test/Sema/constexpr.c
@@ -312,7 +312,7 @@ constexpr int *V84 = 42;
// expected-error at -1 {{constexpr pointer initializer is not null}}
constexpr int *V85 = nullptr;
constexpr int *V91 = 0.0;
-// expected-error at -1 {{constexpr pointer initializer is not null}}
+// expected-error at -1 {{initializing 'int *const' with an expression of incompatible type 'double'}}
// Check that constexpr variables should not be VLAs.
void f6(const int P1) {
>From b73fa9afe2bd6cbfec0a679a1d5d63a41cc645dd Mon Sep 17 00:00:00 2001
From: Samrudh Nelli <samrudhnelli at gmail.com>
Date: Thu, 12 Feb 2026 20:06:40 +0530
Subject: [PATCH 6/7] chore: delete the previous unnecessary comment
I feel the code is self explanatory and doesnt require a comment.
---
clang/lib/Sema/SemaInit.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 9b6162e51a0ac..3480499fd11b4 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -8503,7 +8503,6 @@ ExprResult InitializationSequence::Perform(Sema &S,
Expr::EvalResult ER;
if (Entity.getType()->getAs<PointerType>() &&
CurInit.get()->EvaluateAsRValue(ER, S.Context) &&
- // Error if the result is not a pointer or not a null pointer
(ER.Val.isLValue() && !ER.Val.isNullPointer())) {
S.Diag(Kind.getLocation(), diag::err_c23_constexpr_pointer_not_null);
return ExprError();
>From d9d800faeec0cc59f772f61a38a98c6c46fe8cad Mon Sep 17 00:00:00 2001
From: Samrudh Nelli <74651507+SamrudhNelli at users.noreply.github.com>
Date: Mon, 16 Feb 2026 18:44:43 +0530
Subject: [PATCH 7/7] Update clang/docs/ReleaseNotes.rst
Co-authored-by: Mariya Podchishchaeva <mariya.podchishchaeva at intel.com>
---
clang/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6c55fcd8a9038..67b074244ea6e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -274,7 +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)
+- Fixed a crash when initializing a ``constexpr`` pointer with a floating-point literal in C23. (#GH180313)
OpenACC Specific Changes
------------------------
More information about the cfe-commits
mailing list