BrunoLevy / geogram

a programming library with geometric algorithms
Other
1.9k stars 129 forks source link

vector segfaults on out-of-memory error #16

Open Yitzi2 opened 2 years ago

Yitzi2 commented 2 years ago

If the system does not have enough memory to allocate a GEO::vector, it will segfault. The reason for this is that GEO::vector is essentially an std::vector that uses the Geogram aligned allocator, and the contract for allocators for STL container classes requires that if they return (i.e. do not throw) they return a valid block of memory (https://stackoverflow.com/questions/4826838/do-standard-library-stl-containers-support-a-form-of-nothrow-allocation).

The Geogram aligned allocator does not fulfill this contract; if it cannot allocate, aligned_malloc will return 0, and this will be returned by the allocator, causing a segfault.

This could be fixed by having the allocate() method check for nullptr and throw std::bad_alloc if it is; that way, the contract will be fulfilled, and an out-of-memory will throw an exception that can be caught higher up.

BrunoLevy commented 2 years ago

Is this really a big problem ? It is possible to do so, but it will slow down certain operations significantly ...

BrunoLevy commented 1 year ago