Skip to content

Commit 69e5f87

Browse files
committed
DPL: fix thread safety of slot allocation in runtime_error_f
1 parent 91acb9d commit 69e5f87

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

Framework/Foundation/src/RuntimeError.cxx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ RuntimeError& error_from_ref(RuntimeErrorRef ref)
6060
RuntimeErrorRef runtime_error_f(const char* format, ...)
6161
{
6262
int i = 0;
63-
bool expected = false;
64-
while (gErrorBooking[i].compare_exchange_strong(expected, true) == false) {
63+
while (true) {
64+
bool expected = false;
65+
if (gErrorBooking[i].compare_exchange_strong(expected, true) == false) {
66+
break;
67+
}
6568
++i;
6669
if (i >= RuntimeError::MAX_RUNTIME_ERRORS) {
6770
throw std::runtime_error("Too many o2::framework::runtime_error thrown without proper cleanup.");
@@ -78,11 +81,18 @@ RuntimeErrorRef runtime_error_f(const char* format, ...)
7881
RuntimeErrorRef runtime_error(const char* s)
7982
{
8083
int i = 0;
81-
bool expected = false;
82-
while (gErrorBooking[i].compare_exchange_strong(expected, true) == false) {
84+
while (true) {
85+
bool expected = false;
86+
if (gErrorBooking[i].compare_exchange_strong(expected, true) == false) {
87+
break;
88+
}
8389
++i;
90+
if (i >= RuntimeError::MAX_RUNTIME_ERRORS) {
91+
throw std::runtime_error("Too many o2::framework::runtime_error thrown without proper cleanup.");
92+
}
8493
}
85-
strncpy(gError[i].what, s, RuntimeError::MAX_RUNTIME_ERROR_SIZE);
94+
strncpy(gError[i].what, s, RuntimeError::MAX_RUNTIME_ERROR_SIZE - 1);
95+
gError[i].what[RuntimeError::MAX_RUNTIME_ERROR_SIZE - 1] = 0;
8696
gError[i].maxBacktrace = canDumpBacktrace() ? backtrace(gError[i].backtrace, BacktraceHelpers::MAX_BACKTRACE_SIZE) : 0;
8797
return RuntimeErrorRef{i};
8898
}

0 commit comments

Comments
 (0)