[PATCH] D149013: [clang][Interp] Check pointers when accessing base class

Corentin Jabot via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 10 06:21:14 PDT 2023


cor3ntin added inline comments.


================
Comment at: clang/test/AST/Interp/records.cpp:509-512
+  constexpr A *a2 = &b + 1; // expected-error {{must be initialized by a constant expression}} \
+                            // expected-note {{cannot access base class of pointer past the end of object}} \
+                            // ref-error {{must be initialized by a constant expression}} \
+                            // ref-note {{cannot access base class of pointer past the end of object}}
----------------
aaron.ballman wrote:
> tbaeder wrote:
> > cor3ntin wrote:
> > > aaron.ballman wrote:
> > > > I may have jumped the gun on accepting this, actually. Forming the pointer to `&b + 1` is fine, but evaluating it by dereferencing it would be UB. e.g., http://eel.is/c++draft/expr.const#13.3
> > > We probably want tests that ensure `&b+2` is invalid in all cases
> > It's being evaluated because the base class is accessed, isn't it? if `b` was of type `A*`, we would not emit a diagnostic (see line 506 above).
> I'm not seeing the access to the base class -- we're forming a pointer, not dereferencing it.
> 
> https://godbolt.org/z/vxThqczeo
> 
> I think this is an existing Clang bug. CC @hubert.reinterpretcast @rsmith for additional opinions.
I believe this is fine, or at least i can't find any wording that would say it's not.

In particular: 
 - taking the address of &b is fine
 - adding + 1 to it is fine: http://eel.is/c++draft/expr#add-4.2
 - casting it to a pointer to base is fine http://eel.is/c++draft/conv.ptr#3
 - treating the whole expression as a constant expression is also fine http://eel.is/c++draft/expr.const#13.3 

So i do believe GCC, MSVC and ICC are correct here.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D149013/new/

https://reviews.llvm.org/D149013



More information about the cfe-commits mailing list