[clang] [clang][ExprConst] Reject field access with nullptr base (PR #113885)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 28 02:09:34 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
Reject them if the base is null, not only if the entire pointer is null.
---
Full diff: https://github.com/llvm/llvm-project/pull/113885.diff
2 Files Affected:
- (modified) clang/lib/AST/ExprConstant.cpp (+1-1)
- (modified) clang/test/CXX/expr/expr.const/p2-0x.cpp (+2-1)
``````````diff
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 8e36cad2d2c6e7..a6694bc0641508 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -1703,7 +1703,7 @@ namespace {
bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) {
if (Designator.Invalid)
return false;
- if (IsNullPtr) {
+ if (getLValueBase().isNull()) {
GenDiag();
Designator.setInvalid();
return false;
diff --git a/clang/test/CXX/expr/expr.const/p2-0x.cpp b/clang/test/CXX/expr/expr.const/p2-0x.cpp
index 767eee1c74f054..67160ba571f33c 100644
--- a/clang/test/CXX/expr/expr.const/p2-0x.cpp
+++ b/clang/test/CXX/expr/expr.const/p2-0x.cpp
@@ -188,7 +188,7 @@ namespace UndefinedBehavior {
namespace Ptr {
struct A {};
- struct B : A { int n; };
+ struct B : A { int n; int m; };
B a[3][3];
constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
B b = {};
@@ -204,6 +204,7 @@ namespace UndefinedBehavior {
static_assert((A*)nb == 0, "");
static_assert((B*)na == 0, "");
constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
+ constexpr const int &mf = nb->m; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
constexpr const int *np1 = (int*)nullptr + 0; // ok
constexpr const int *np2 = &(*(int(*)[4])nullptr)[0]; // ok
constexpr const int *np3 = &(*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot perform pointer arithmetic on null pointer}}
``````````
</details>
https://github.com/llvm/llvm-project/pull/113885
More information about the cfe-commits
mailing list