Skip to content

Conversation

@SchrodingerZhu
Copy link
Contributor

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.

@llvmbot llvmbot added the libc label Dec 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 15, 2025

@llvm/pr-subscribers-libc

Author: Schrodinger ZHU Yifan (SchrodingerZhu)

Changes

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 --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) {

@github-actions
Copy link

github-actions bot commented Dec 15, 2025

🐧 Linux x64 Test Results

✅ The build succeeded and no tests ran. This is expected in some build configurations.

@github-actions
Copy link

github-actions bot commented Dec 15, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@SchrodingerZhu SchrodingerZhu merged commit 10ed050 into llvm:main Dec 17, 2025
26 checks passed
@SchrodingerZhu SchrodingerZhu deleted the libc/string-fix branch December 17, 2025 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants