fix(libc): call the new handler when operator new fails to allocate #1846
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1846"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by NahuelGenchi on 2026-10-08T21:36:52Z.
What
std::set_new_handlerwas implemented in libc butoperator newnever called the handler. When the allocator returned null,operator newthrewbad_allocstraight away. The C++ standard ([new.delete.single]) saysoperator newloops on failure: it calls the current new handler and tries again, and it only throwsbad_alloconce there is no handler. A title that frees a cache or a reserve inside its handler, which is the usual reason to install one, would crash on the first out of memory condition instead of recovering.AllocateinStdioExtras.cppnow follows that loop foroperator new,operator new[]and the two nothrow forms. The handler runs outside the allocator callback, so it may allocate, free or reinstall itself. A handler that throwsbad_allocpropagates from the throwing forms and gives a null pointer from the nothrow ones. The handler slot is atomic now, sinceset_new_handlerandget_new_handlerare required to be thread safe.Tested
Ubuntu 26.04, GCC 15.2. The new
guest_new_handlertest installs a replacement allocator that fails a chosen number of times and checks each form: no handler (throws, or null for nothrow), a handler that returns (retried exactly as often as it fails), a handler that uninstalls itself after three calls, a handler that throwsbad_alloc, a handler that allocates, the previous handler returned byset_new_handler, and a zero byte request. It passes with the change, and without the change it dies with an uncaughtbad_alloc. The non-GPU suite passes (241 tests) and the convention checks pass. Not run on Windows locally, so the Windows job is the first run there.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by rmz-24 on 2026-10-09T12:00:17Z.
Windows run, since this was tested on Ubuntu only: Windows 11 Pro 25H2 (10.0.26200.9457), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release.
81d86aa5cherry-picked onto currentmaind70b8998(applies cleanly), full build pluslibs.guest_new_handlerpasses.StdioExtras.cppreset tomainand the new test kept,guest_new_handlerfails as expected:std::bad_allocescapes on the first failed allocation, because no handler is called.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T04:18:39Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/tests/GuestNewHandler.cpp