C++ Logo

std-proposals

Advanced search

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

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Sun, 27 Oct 2024 23:04:55 +0100
Hi Jens, if one can (in theory) call member functions on a nullptr, then those are either not using the this-object or are static, as there is no usable object. (Any other interpretation like having a default object would be too far-fetched and can be achieved by just creating a normal object and use it by default.) 315 brought up both cases: The non-static member function as example and the static member function in the text. As far as I understand the non-static member function case was explicitly UB in the standard, but the static member function case was unclear, as although nullptr->staticf() leads to (*nullptr).staticf(), the left side of which was definitely evaluated, but not necessarily dereferenced.   In 315 there is an older resolution that (even?) the shown (non-static?) example is valid. 2823 made it clear that (*nullptr) is always UB.   Right?   Best, Sebastian   -----Ursprüngliche Nachricht----- Von:Jens Maurer <jens.maurer_at_[hidden]> Gesendet:So 27.10.2024 22:34 Betreff:Re: [std-proposals] Calling methods on a nullptr An:std-proposals_at_[hidden]; CC:Sebastian Wittmeier <wittmeier_at_[hidden]>; 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 22:05:20