[libc][CPP] make the string trap on OOM (#172260)

This PR makes the string trap on OOM.

Previously, the `__builtin_unreachable` has made debugging tricky as it
makes the control flow of OOM as an undefined behavior.
We can run into OOM with testing configuration easily where memory is
statically bounded.

We did not settle with the best solution of this but making it trap is
at least better than UB
in this case.
This commit is contained in:
Schrodinger ZHU Yifan 2025-12-17 05:03:39 -05:00 committed by GitHub
parent a6f837e9f8
commit 10ed050b34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -132,13 +132,16 @@ public:
// by 8 is cheap. We guard the extension so the operation doesn't overflow.
if (new_capacity < SIZE_MAX / 11)
new_capacity = new_capacity * 11 / 8;
if (void *Ptr = ::realloc(buffer_ == get_empty_string() ? nullptr : buffer_,
new_capacity)) {
buffer_ = static_cast<char *>(Ptr);
capacity_ = new_capacity;
} else {
__builtin_unreachable(); // out of memory
return;
}
// Out of memory: this is not handled in current implementation,
// We trap the program and exits.
__builtin_trap();
}
LIBC_INLINE void resize(size_t size) {