libstdc++: Fix exception safety in pmr::unsynchronized_pool_resource
Fixes a crash in pmr::unsynchronized_pool_resource by ensuring exceptions during initial allocation are properly handled.
A bug in pmr::unsynchronized_pool_resource could lead to a crash due to improper exception handling during initial memory allocation for pool chunks. If allocation failed, the exception was swallowed, leaving the pool in a corrupted state with an incorrect capacity. This commit corrects the allocation logic to rethrow exceptions on failure and ensures the capacity is only updated if the allocation succeeds, thus maintaining the pool’s integrity and fixing the crash.
In Details
This commit addresses an exception-safety issue in the memory resource pool implementation within libstdc++. Specifically, in pmr::unsynchronized_pool_resource's _Pool::replenish and _Pool::vector::insert methods, the initial allocation of memory for pool chunks was susceptible to failure. If new threw an exception, it was caught and suppressed, but the pool's capacity member was updated regardless, leading to an inconsistent state. The fix ensures that exceptions during initial allocation are rethrown and that capacity is only updated upon successful allocation. This guarantees t…
- libstdc++
- The standard C++ library implementation used by GCC. It provides the C++ standard library components, including containers, algorithms, and I/O facilities.
- pmr
- Polymorphic Memory Resources. A C++17 feature allowing different memory allocation strategies to be plugged into standard containers and other types.
- unsynchronized_pool_resource
- A C++ standard library class that manages a pool of memory for allocations. It is not thread-safe, offering potentially higher performance in single-threaded contexts.
- exception safety
- A property of C++ code that guarantees the program remains in a valid state even when exceptions are thrown during execution. Different levels of exception safety exist (basic, strong, no-throw).
- allocation
- The process of reserving a block of memory for use by a program.
- capacity
- In the context of a pool resource, this refers to the amount of memory that the pool can currently manage or has allocated.