Skip to content

Commit 6b00297

Browse files
Replace assert-based reentrancy guard with atomic spinlock in ed25519_verify to ensure thread safety during concurrent calls.
1 parent 837c561 commit 6b00297

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

src/Identity.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "Identity.h"
22
#include <string.h>
3-
#include <assert.h>
3+
#include <atomic>
44
#define ED25519_NO_SEED 1
55
#include <ed_25519.h>
66

@@ -16,12 +16,11 @@ Identity::Identity(const char* pub_hex) {
1616

1717
bool Identity::verify(const uint8_t* sig, const uint8_t* message, int msg_len) const {
1818
// ed25519_verify uses static buffers internally (ge.c) and is NOT reentrant.
19-
// This guard catches concurrent calls (e.g. from multiple FreeRTOS tasks).
20-
static volatile bool in_verify = false;
21-
assert(!in_verify && "ed25519_verify is not reentrant - concurrent call detected");
22-
in_verify = true;
19+
// Spinlock to serialize concurrent calls (e.g. from multiple FreeRTOS tasks).
20+
static std::atomic<bool> in_verify{false};
21+
while (in_verify.exchange(true, std::memory_order_acquire)) { /* spin */ }
2322
bool result = ed25519_verify(sig, message, msg_len, pub_key);
24-
in_verify = false;
23+
in_verify.store(false, std::memory_order_release);
2524
return result;
2625
}
2726

0 commit comments

Comments
 (0)