From 5e0d8d879d751f7a7acf21e6f1d4c295e40ea323 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sun, 14 Mar 2021 22:04:11 +0530 Subject: [PATCH] crypto: use macro map for NodeCryptoError --- src/crypto/crypto_util.h | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/crypto/crypto_util.h b/src/crypto/crypto_util.h index 1a7bef77a40930..f2dc67afdfd07c 100644 --- a/src/crypto/crypto_util.h +++ b/src/crypto/crypto_util.h @@ -159,13 +159,18 @@ void Decode(const v8::FunctionCallbackInfo& args, } } +#define NODE_CRYPTO_ERROR_CODES_MAP(V) \ + V(CIPHER_JOB_FAILED, "Cipher job failed") \ + V(DERIVING_BITS_FAILED, "Deriving bits failed") \ + V(ENGINE_NOT_FOUND, "Engine \"%s\" was not found") \ + V(INVALID_KEY_TYPE, "Invalid key type") \ + V(KEY_GENERATION_JOB_FAILED, "Key generation job failed") \ + V(OK, "Ok") \ + enum class NodeCryptoError { - CIPHER_JOB_FAILED, - DERIVING_BITS_FAILED, - ENGINE_NOT_FOUND, - INVALID_KEY_TYPE, - KEY_GENERATION_JOB_FAILED, - OK +#define V(CODE, DESCRIPTION) CODE, + NODE_CRYPTO_ERROR_CODES_MAP(V) +#undef V }; // Utility struct used to harvest error information from openssl's error stack @@ -194,28 +199,15 @@ template void CryptoErrorStore::Insert(const NodeCryptoError error, Args&&... args) { const char* error_string = nullptr; switch (error) { - case NodeCryptoError::CIPHER_JOB_FAILED: - error_string = "Cipher job failed"; - break; - case NodeCryptoError::DERIVING_BITS_FAILED: - error_string = "Deriving bits failed"; - break; - case NodeCryptoError::ENGINE_NOT_FOUND: - error_string = "Engine \"%s\" was not found"; - break; - case NodeCryptoError::INVALID_KEY_TYPE: - error_string = "Invalid key type"; - break; - case NodeCryptoError::KEY_GENERATION_JOB_FAILED: - error_string = "Key generation failed"; - break; - case NodeCryptoError::OK: - error_string = "Ok"; - break; +#define V(CODE, DESCRIPTION) \ + case NodeCryptoError::CODE: error_string = DESCRIPTION; break; + NODE_CRYPTO_ERROR_CODES_MAP(V) +#undef V } errors_.emplace_back(SPrintF(error_string, std::forward(args)...)); } +#undef NODE_CRYPTO_ERROR_CODES_MAP template T* MallocOpenSSL(size_t count) {