Skip to content

Commit 1685e7f

Browse files
authored
[Support] Fix crash in install_bad_alloc_error_handler (#83160)
Previously, the function `install_bad_alloc_error_handler` was asserting that a bad_alloc error handler was not already installed. However, it was actually checking for the fatal-error handler, not for the bad_alloc error handler, causing an assertion failure if a fatal-error handler was already installed. Fixes #83040
1 parent 1a0c988 commit 1685e7f

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

llvm/lib/Support/ErrorHandling.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler,
130130
#if LLVM_ENABLE_THREADS == 1
131131
std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
132132
#endif
133-
assert(!ErrorHandler && "Bad alloc error handler already registered!\n");
133+
assert(!BadAllocErrorHandler &&
134+
"Bad alloc error handler already registered!\n");
134135
BadAllocErrorHandler = handler;
135136
BadAllocErrorHandlerUserData = user_data;
136137
}

llvm/unittests/Support/ErrorTest.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -1132,4 +1132,30 @@ TEST(Error, moveInto) {
11321132
}
11331133
}
11341134

1135+
TEST(Error, FatalBadAllocErrorHandlersInteraction) {
1136+
auto ErrorHandler = [](void *Data, const char *, bool) {};
1137+
install_fatal_error_handler(ErrorHandler, nullptr);
1138+
// The following call should not crash; previously, a bug in
1139+
// install_bad_alloc_error_handler asserted that no fatal-error handler is
1140+
// installed already.
1141+
install_bad_alloc_error_handler(ErrorHandler, nullptr);
1142+
1143+
// Don't interfere with other tests.
1144+
remove_fatal_error_handler();
1145+
remove_bad_alloc_error_handler();
1146+
}
1147+
1148+
TEST(Error, BadAllocFatalErrorHandlersInteraction) {
1149+
auto ErrorHandler = [](void *Data, const char *, bool) {};
1150+
install_bad_alloc_error_handler(ErrorHandler, nullptr);
1151+
// The following call should not crash; related to
1152+
// FatalBadAllocErrorHandlersInteraction: Ensure that the error does not occur
1153+
// in the other direction.
1154+
install_fatal_error_handler(ErrorHandler, nullptr);
1155+
1156+
// Don't interfere with other tests.
1157+
remove_fatal_error_handler();
1158+
remove_bad_alloc_error_handler();
1159+
}
1160+
11351161
} // namespace

0 commit comments

Comments
 (0)