[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:
parent
a6f837e9f8
commit
10ed050b34
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user