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

via libc-commits libc-commits at lists.llvm.org
Mon Dec 15 00:01:57 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Schrodinger ZHU Yifan (SchrodingerZhu)

<details>
<summary>Changes</summary>

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.


---
Full diff: https://github.com/llvm/llvm-project/pull/172260.diff


1 Files Affected:

- (modified) libc/src/__support/CPP/string.h (+3-2) 


``````````diff
diff --git a/libc/src/__support/CPP/string.h b/libc/src/__support/CPP/string.h
index 1ac04c7f1f9dc..a4de1ace02d22 100644
--- a/libc/src/__support/CPP/string.h
+++ b/libc/src/__support/CPP/string.h
@@ -136,9 +136,10 @@ class string {
                               new_capacity)) {
       buffer_ = static_cast<char *>(Ptr);
       capacity_ = new_capacity;
-    } else {
-      __builtin_unreachable(); // out of memory
     }
+    // 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) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/172260


More information about the libc-commits mailing list