Skip to content

Commit b9248dc

Browse files
anonrigaduh95
authored andcommitted
crypto: skip clearing empty OpenSSL error queues
ClearErrorOnReturn calls ERR_clear_error() when it is created and again when it goes out of scope. That function resets all 16 slots of the thread's error queue, including two free() calls per slot, even when the queue is empty, which is almost always the case. Check for a queued error first, which costs a fraction of that. ClearErrorOnReturn guards most crypto operations, including every CryptoJob, so this applies to all of them. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Grok PR-URL: #67109 Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent 216f123 commit b9248dc

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

‎deps/ncrypto/ncrypto.cc‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,22 @@ constexpr BoringSSLCipher kBoringSSLCiphers[] = {
337337

338338
// ============================================================================
339339

340+
namespace {
341+
// ERR_clear_error() resets every slot of the error queue even when it is
342+
// empty, which costs several times more than checking for an error first.
343+
void ClearErrorQueue() {
344+
if (ERR_peek_error() != 0) ERR_clear_error();
345+
}
346+
} // namespace
347+
340348
ClearErrorOnReturn::ClearErrorOnReturn(CryptoErrorList* errors)
341349
: errors_(errors) {
342-
ERR_clear_error();
350+
ClearErrorQueue();
343351
}
344352

345353
ClearErrorOnReturn::~ClearErrorOnReturn() {
346354
if (errors_ != nullptr) errors_->capture();
347-
ERR_clear_error();
355+
ClearErrorQueue();
348356
}
349357

350358
unsigned long ClearErrorOnReturn::peekError() { // NOLINT(runtime/int)

0 commit comments

Comments
 (0)