Skip to content

Commit 66d4241

Browse files
committed
[clang][ExprConst] Reject field access with nullptr base
1 parent 9ae41c2 commit 66d4241

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,7 @@ namespace {
17031703
bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) {
17041704
if (Designator.Invalid)
17051705
return false;
1706-
if (IsNullPtr) {
1706+
if (getLValueBase().isNull()) {
17071707
GenDiag();
17081708
Designator.setInvalid();
17091709
return false;

clang/test/CXX/expr/expr.const/p2-0x.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ namespace UndefinedBehavior {
188188

189189
namespace Ptr {
190190
struct A {};
191-
struct B : A { int n; };
191+
struct B : A { int n; int m; };
192192
B a[3][3];
193193
constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
194194
B b = {};
@@ -204,6 +204,7 @@ namespace UndefinedBehavior {
204204
static_assert((A*)nb == 0, "");
205205
static_assert((B*)na == 0, "");
206206
constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
207+
constexpr const int &mf = nb->m; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
207208
constexpr const int *np1 = (int*)nullptr + 0; // ok
208209
constexpr const int *np2 = &(*(int(*)[4])nullptr)[0]; // ok
209210
constexpr const int *np3 = &(*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot perform pointer arithmetic on null pointer}}

0 commit comments

Comments
 (0)