Skip to content

abort() on malloc() failure in new with exceptions disabled#11079

Merged
kripken merged 10 commits intomasterfrom
new
May 5, 2020
Merged

abort() on malloc() failure in new with exceptions disabled#11079
kripken merged 10 commits intomasterfrom
new

Conversation

@kripken
Copy link
Member

@kripken kripken commented May 4, 2020

When libc++/libc++abi are built with exceptions disabled, the
new implementation there does not throw an exception for
an error, but it also does nothing else. So new ends up returning
a zero if malloc did, which can break programs.

Technically libc++/libc++abi are doing a reasonable thing here,
just removing all exceptions-related code when exceptions are
disabled. The assumption is likely that a user program would
set a new_handler if an error is desired. For us, we have to change
this as our default mode is to have exceptions disabled, and we
don't want users to need to know they need to do anything.

This makes it abort instead. (Note that without growth this
happened to always work, since we abort on any failing allocation.
With growth enabled, though, malloc returns 0, and we end up
in this situation.)

Fixes #11042 As discussed there I also looked at the option of
installing a set_new_handler that does an abort. That ends up
increasing code size by a little (bit less than 1%) because if adds
a global constructor, a function to the table, and some memory
operations. It seems better to just modify new itself which avoids
all that.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OOM during std::vector allocation doesn't terminate the program

2 participants