Date: Mon, 4 Aug 2025 12:32:20 +0100
On Mon, Aug 4, 2025 at 12:09 PM Andrey Semashev wrote:
>
> > You're missing the point. If the above function were to be marked as
> > 'throw_on_nullptr', and then if the invocation of 'strstr' returns a
> > nullptr and has 3 added to it, it will throw an exception of type
> > nullptr_t, rather than segfault when 'libc' inside the implementation
> > of 'stoul' tries to access the memory address 0x0000000000000003.
>
> And what if the argument p was nullptr in the first place?
nullptr_t will be thrown if a nullptr is passed as an argument to any
function. If you genuinely need to pass a nullptr to a function
though, you must exempt it:
void Func(char const *const p) throw_on_nullptr
{
return SomeOtherFunction( std::exempt(p) );
}
>
> > You're missing the point. If the above function were to be marked as
> > 'throw_on_nullptr', and then if the invocation of 'strstr' returns a
> > nullptr and has 3 added to it, it will throw an exception of type
> > nullptr_t, rather than segfault when 'libc' inside the implementation
> > of 'stoul' tries to access the memory address 0x0000000000000003.
>
> And what if the argument p was nullptr in the first place?
nullptr_t will be thrown if a nullptr is passed as an argument to any
function. If you genuinely need to pass a nullptr to a function
though, you must exempt it:
void Func(char const *const p) throw_on_nullptr
{
return SomeOtherFunction( std::exempt(p) );
}
Received on 2025-08-04 11:32:28