[libcxx-commits] [PATCH] D150610: [libc++] Make sure `operator new` never returns nullptr, even under -fno-exceptions
Louis Dionne via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jun 14 13:20:56 PDT 2023
ldionne added a comment.
In D150610#4418324 <https://reviews.llvm.org/D150610#4418324>, @probinson wrote:
>> The Standard has a few requirements for the allocation functions, some of which are impossible to satisfy under -fno-exceptions:
>>
>> 1. operator new(size_t) must never return nullptr
>> 2. operator new(size_t, nothrow_t) must call the throwing version and return nullptr on failure to allocate
>> 3. We can't throw exceptions when compiled with -fno-exceptions
>
> Feels like building the library with `-fno-exceptions` is just plain non-conforming?
Yes, I think that's accurate. We basically don't have a way to be conforming when we build the library itself with `-fno-exceptions`. @jwakely's suggestion makes us very close to that, in the sense that `operator new(size_t)` will behave as expected, and `operator new(size_t, nothrow_t)` will give the impression that we're calling `operator new(size_t)`. The only remaining problem is what happens if the library is built with `-fno-exceptions` and the user overrides `operator new(size_t)` *but not* `operator new(size_t, nothrow_t)`. In that case, our `operator new(size_t, nothrow_t)` will call the user's `operator new(size_t)` (which must terminate-on-failure in order to be conforming), and we'll still be non-conforming the same way we are today.
I think what I would really like is for a relaxation that does not force `operator new(size_t, nothrow_t)` to call `operator new(size_t)`, since implementations effectively cannot do that. That would weaken a useful guarantee provided by the standard for the benefit of platforms that use `-fno-exceptions` -- I'm not sure that's the right tradeoff and even less sure that's something WG21 would ever consider.
> Requirement 2 seems odd, the only justification I can come up with is to allow replacing _only_ the throwing version, and having the nothrow version still Just Work in a compatible way. Naively I'd have thought the other way around would make more sense (throwing version calls nothrow version and turns nullptr into throw/abort) but that's not what got standardized.
Yes and yes, I agree and I think it would have made more sense. However, I think changing that is impossible at this point.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D150610/new/
https://reviews.llvm.org/D150610
More information about the libcxx-commits
mailing list