Skip to content

Crash in install_bad_alloc_error_handler #83040

Closed
@fabianbs96

Description

@fabianbs96

The function install_bad_alloc_error_handler asserts that a bad_alloc error handler is not already installed.
However, it actually checks for the fatal-error handler, not for the bad_alloc error handler, causing an assertion failure if a fatal-error handler is already installed.
Correct behavior would be to only crash if a bad_alloc handler is already installed, making it independent from fatal-error handlers.

Minimal working example:

#include <llvm/Support/ErrorHandling.h>

int main() {
    llvm::install_fatal_error_handler([](void*,const char*,bool){});
    llvm::install_bad_alloc_error_handler([](void*,const char*,bool){}); // <-- Crash occurs here
}

Propsed Fix:

diff --git a/llvm/lib/Support/ErrorHandling.cpp b/llvm/lib/Support/ErrorHandling.cpp
index b8b3b7424ac6..c268543eb1fd 100644
--- a/llvm/lib/Support/ErrorHandling.cpp
+++ b/llvm/lib/Support/ErrorHandling.cpp
@@ -130,7 +130,7 @@ void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler,
 #if LLVM_ENABLE_THREADS == 1
   std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
 #endif
-  assert(!ErrorHandler && "Bad alloc error handler already registered!\n");
+  assert(!BadAllocErrorHandler && "Bad alloc error handler already registered!\n");
   BadAllocErrorHandler = handler;
   BadAllocErrorHandlerUserData = user_data;
 }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions