Skip to content

Commit b80965e

Browse files
authored
[Support] Report OOM from allocate_buffer (#85449)
Previously, it called `::operator new` which may throw `std::bad_alloc`, regardless of whether LLVM itself was built with exception handling, and this can cause safety issues if outside code has destructors that will call back into LLVM. Now we use `::operator new(..., nothrow)` and call `llvm::report_bad_alloc_error` when allocation fails, which will abort when LLVM is built without exceptions. Ref: #85281
1 parent 08c3642 commit b80965e

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

llvm/lib/Support/MemAlloc.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313

1414
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void *
1515
llvm::allocate_buffer(size_t Size, size_t Alignment) {
16-
return ::operator new(Size
16+
void *Result = ::operator new(Size,
1717
#ifdef __cpp_aligned_new
18-
,
19-
std::align_val_t(Alignment)
18+
std::align_val_t(Alignment),
2019
#endif
21-
);
20+
std::nothrow);
21+
if (Result == nullptr) {
22+
report_bad_alloc_error("Buffer allocation failed");
23+
}
24+
return Result;
2225
}
2326

2427
void llvm::deallocate_buffer(void *Ptr, size_t Size, size_t Alignment) {

0 commit comments

Comments
 (0)