Skip to content

Commit de066be

Browse files
committed
[clang][ExprConst] Reject field access with nullptr base
1 parent d0d726e commit de066be

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3261,8 +3261,8 @@ static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj,
32613261
RL = &Info.Ctx.getASTRecordLayout(Derived);
32623262
}
32633263

3264-
Obj.getLValueOffset() += RL->getBaseClassOffset(Base);
32653264
Obj.addDecl(Info, E, Base, /*Virtual*/ false);
3265+
Obj.getLValueOffset() += RL->getBaseClassOffset(Base);
32663266
return true;
32673267
}
32683268

@@ -3286,8 +3286,8 @@ static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj,
32863286
// Find the virtual base class.
32873287
if (DerivedDecl->isInvalidDecl()) return false;
32883288
const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl);
3289-
Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl);
32903289
Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true);
3290+
Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl);
32913291
return true;
32923292
}
32933293

@@ -3330,8 +3330,8 @@ static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal,
33303330
}
33313331

33323332
unsigned I = FD->getFieldIndex();
3333-
LVal.adjustOffset(Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)));
33343333
LVal.addDecl(Info, E, FD);
3334+
LVal.adjustOffset(Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)));
33353335
return true;
33363336
}
33373337

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)