Skip to content

Commit 10ed050

Browse files
[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.
1 parent a6f837e commit 10ed050

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

libc/src/__support/CPP/string.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,16 @@ class string {
132132
// by 8 is cheap. We guard the extension so the operation doesn't overflow.
133133
if (new_capacity < SIZE_MAX / 11)
134134
new_capacity = new_capacity * 11 / 8;
135+
135136
if (void *Ptr = ::realloc(buffer_ == get_empty_string() ? nullptr : buffer_,
136137
new_capacity)) {
137138
buffer_ = static_cast<char *>(Ptr);
138139
capacity_ = new_capacity;
139-
} else {
140-
__builtin_unreachable(); // out of memory
140+
return;
141141
}
142+
// Out of memory: this is not handled in current implementation,
143+
// We trap the program and exits.
144+
__builtin_trap();
142145
}
143146

144147
LIBC_INLINE void resize(size_t size) {

0 commit comments

Comments
 (0)