[libc-commits] [libc] 10ed050 - [libc][CPP] make the string trap on OOM (#172260)
via libc-commits
libc-commits at lists.llvm.org
Wed Dec 17 02:03:44 PST 2025
Author: Schrodinger ZHU Yifan
Date: 2025-12-17T05:03:39-05:00
New Revision: 10ed050b34bd46935c3c2418b60a7d3a5f1c451c
URL: https://github.com/llvm/llvm-project/commit/10ed050b34bd46935c3c2418b60a7d3a5f1c451c
DIFF: https://github.com/llvm/llvm-project/commit/10ed050b34bd46935c3c2418b60a7d3a5f1c451c.diff
LOG: [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.
Added:
Modified:
libc/src/__support/CPP/string.h
Removed:
################################################################################
diff --git a/libc/src/__support/CPP/string.h b/libc/src/__support/CPP/string.h
index 1ac04c7f1f9dc..04f0e9538d03e 100644
--- a/libc/src/__support/CPP/string.h
+++ b/libc/src/__support/CPP/string.h
@@ -132,13 +132,16 @@ class string {
// 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) {
More information about the libc-commits
mailing list