C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Calling methods on a nullptr

From: Jens Maurer <jens.maurer_at_[hidden]>
Date: Sun, 27 Oct 2024 22:34:23 +0100
On 27/10/2024 22.10, Sebastian Wittmeier via Std-Proposals wrote:
> That was legal C++, it was retroactively made UB on purpose a year ago (as far as I understand):
>
> https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_closed.html#315
>
> https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2823

No.

Issue 315 is concerning static member functions, which are not being
discussed here, I think.

And CWG2823 clarified the status quo by making undefined behavior
explicit, but didn't change the rules.

The prior text said "denoting the object or function to which the operand points"
Since a null pointer value doesn't point to any object, it's left undefined
what this operation does. CWG2823 has made that undefined behavior explicit.

Jens


> -----Ursprüngliche Nachricht-----
> *Von:* Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]>
> *Gesendet:* So 27.10.2024 21:16
> *Betreff:* [std-proposals] Calling methods on a nullptr
> *An:* std-proposals <std-proposals_at_[hidden]>;
> *CC:* Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>;
> This is a totally half-baked idea, but I just want to float it here
> sort of like a thought experiment to see if anyone is intrigued by the
> possibilities it could open up.
>
> C++ could potentially allow the invocation of a method on a nullptr.
> For example:
>
> class MyClass {
> string name;
> public:
> string GetName(void)
> {
> if ( nullptr == this ) return {};
> return this->name;
> }
> };
>
> It gets a little more complicated if the method is virtual, for example:
>
> class MyClass {
> string name;
> public:
> virtual string GetName(void)
> {
> if ( nullptr == this ) return {};
> return this->name;
> }
> };
>
> Because if we supply the following standalone function to an x86_64
> compiler on Linux:
>
> string Monkey(MyClass *const p)
> {
> return p->GetName();
> }
>
> It will give us back something like:
>
> mov r11, rdi # Put the object pointer in R11
> mov r11, [r11] # Put the address of the VTable in R11
> mov r11, [r11] # Put the address of the first method in R11
> jmp r11
>
> This of course will segfault the first time it dereferences R11
> because it's a nullptr.
>
> But if we were to mark the class as nullptr-friendly:
>
> class MyClass friend nullptr {
> string name;
> public:
> virtual string GetName(void)
> {
> if ( nullptr == this ) return {};
> return this->name;
> }
> };
>
> Then our 'Monkey' function would then know to check first whether 'p'
> is a nullptr, and if it is a nullptr, then instead of dereferencing
> 'p' to find a VTable pointer, it instead just uses the VTable of
> MyClass.
>
> So it could be possible in the future in C++ to accommodate the
> invocation of a method on a nullptr.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
>

Received on 2024-10-27 21:34:26