-
Notifications
You must be signed in to change notification settings - Fork 28.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
src: assign error codes to more errors thrown in C++ #20121
Closed
Closed
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
034ddaf
src: add THROW_ERR_* helpers
joyeecheung 495ba98
src: migrate ERR_INDEX_OUT_OF_RANGE in C++
joyeecheung 0a9f327
src: throw ERR_INVALID_ARG_TYPE in C++ argument checks
joyeecheung 185c60d
src: throw ERR_BUFFER_OUT_OF_BOUNDS in node_buffer.cc
joyeecheung b2e7ff3
src: throw ERR_MISSING_MODULE in module_wrap.cc
joyeecheung 46935ab
src: throw ERR_INVALID_ARG_VALUE in node_crypto.cc
joyeecheung 0b4364e
src: throw ERR_MISSING_ARGS in node_crypto.cc
joyeecheung 184e8ea
fixup! src: throw ERR_MISSING_ARGS in node_crypto.cc
joyeecheung 834764e
fixup! src: throw ERR_INVALID_ARG_TYPE in C++ argument checks
joyeecheung 60a0c36
fixup! src: throw ERR_INVALID_ARG_VALUE in node_crypto.cc
joyeecheung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
|
||
#include "node.h" | ||
#include "node_buffer.h" | ||
#include "node_errors.h" | ||
#include "node_constants.h" | ||
#include "node_crypto.h" | ||
#include "node_crypto_bio.h" | ||
|
@@ -45,20 +46,6 @@ | |
#include <memory> | ||
#include <vector> | ||
|
||
#define THROW_AND_RETURN_IF_NOT_BUFFER(val, prefix) \ | ||
do { \ | ||
if (!Buffer::HasInstance(val)) { \ | ||
return env->ThrowTypeError(prefix " must be a buffer"); \ | ||
} \ | ||
} while (0) | ||
|
||
#define THROW_AND_RETURN_IF_NOT_STRING(val, prefix) \ | ||
do { \ | ||
if (!val->IsString()) { \ | ||
return env->ThrowTypeError(prefix " must be a string"); \ | ||
} \ | ||
} while (0) | ||
|
||
static const char PUBLIC_KEY_PFX[] = "-----BEGIN PUBLIC KEY-----"; | ||
static const int PUBLIC_KEY_PFX_LEN = sizeof(PUBLIC_KEY_PFX) - 1; | ||
static const char PUBRSA_KEY_PFX[] = "-----BEGIN RSA PUBLIC KEY-----"; | ||
|
@@ -518,7 +505,7 @@ void SecureContext::SetKey(const FunctionCallbackInfo<Value>& args) { | |
if (args[1]->IsUndefined() || args[1]->IsNull()) | ||
len = 1; | ||
else | ||
THROW_AND_RETURN_IF_NOT_STRING(args[1], "Pass phrase"); | ||
THROW_AND_RETURN_IF_NOT_STRING(env, args[1], "Pass phrase"); | ||
} | ||
|
||
BIO *bio = LoadBIO(env, args[0]); | ||
|
@@ -916,7 +903,7 @@ void SecureContext::SetCiphers(const FunctionCallbackInfo<Value>& args) { | |
return env->ThrowTypeError("Ciphers argument is mandatory"); | ||
} | ||
|
||
THROW_AND_RETURN_IF_NOT_STRING(args[0], "Ciphers"); | ||
THROW_AND_RETURN_IF_NOT_STRING(env, args[0], "Ciphers"); | ||
|
||
const node::Utf8Value ciphers(args.GetIsolate(), args[0]); | ||
SSL_CTX_set_cipher_list(sc->ctx_, *ciphers); | ||
|
@@ -931,7 +918,7 @@ void SecureContext::SetECDHCurve(const FunctionCallbackInfo<Value>& args) { | |
if (args.Length() != 1) | ||
return env->ThrowTypeError("ECDH curve name argument is mandatory"); | ||
|
||
THROW_AND_RETURN_IF_NOT_STRING(args[0], "ECDH curve name"); | ||
THROW_AND_RETURN_IF_NOT_STRING(env, args[0], "ECDH curve name"); | ||
|
||
node::Utf8Value curve(env->isolate(), args[0]); | ||
|
||
|
@@ -989,7 +976,8 @@ void SecureContext::SetOptions(const FunctionCallbackInfo<Value>& args) { | |
ASSIGN_OR_RETURN_UNWRAP(&sc, args.Holder()); | ||
|
||
if (args.Length() != 1 || !args[0]->IntegerValue()) { | ||
return sc->env()->ThrowTypeError("Options must be an integer value"); | ||
return node::THROW_ERR_INVALID_ARG_TYPE( | ||
sc->env(), "Options must be an integer value"); | ||
} | ||
|
||
SSL_CTX_set_options( | ||
|
@@ -1008,7 +996,7 @@ void SecureContext::SetSessionIdContext( | |
return env->ThrowTypeError("Session ID context argument is mandatory"); | ||
} | ||
|
||
THROW_AND_RETURN_IF_NOT_STRING(args[0], "Session ID context"); | ||
THROW_AND_RETURN_IF_NOT_STRING(env, args[0], "Session ID context"); | ||
|
||
const node::Utf8Value sessionIdContext(args.GetIsolate(), args[0]); | ||
const unsigned char* sid_ctx = | ||
|
@@ -1043,8 +1031,8 @@ void SecureContext::SetSessionTimeout(const FunctionCallbackInfo<Value>& args) { | |
ASSIGN_OR_RETURN_UNWRAP(&sc, args.Holder()); | ||
|
||
if (args.Length() != 1 || !args[0]->IsInt32()) { | ||
return sc->env()->ThrowTypeError( | ||
"Session timeout must be a 32-bit integer"); | ||
return node::THROW_ERR_INVALID_ARG_TYPE( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps remove the |
||
sc->env(), "Session timeout must be a 32-bit integer"); | ||
} | ||
|
||
int32_t sessionTimeout = args[0]->Int32Value(); | ||
|
@@ -1085,7 +1073,7 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) { | |
} | ||
|
||
if (args.Length() >= 2) { | ||
THROW_AND_RETURN_IF_NOT_BUFFER(args[1], "Pass phrase"); | ||
THROW_AND_RETURN_IF_NOT_BUFFER(env, args[1], "Pass phrase"); | ||
size_t passlen = Buffer::Length(args[1]); | ||
pass = new char[passlen + 1]; | ||
memcpy(pass, Buffer::Data(args[1]), passlen); | ||
|
@@ -1212,7 +1200,7 @@ void SecureContext::SetTicketKeys(const FunctionCallbackInfo<Value>& args) { | |
return env->ThrowTypeError("Ticket keys argument is mandatory"); | ||
} | ||
|
||
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Ticket keys"); | ||
THROW_AND_RETURN_IF_NOT_BUFFER(env, args[0], "Ticket keys"); | ||
|
||
if (Buffer::Length(args[0]) != 48) { | ||
return env->ThrowTypeError("Ticket keys length must be 48 bytes"); | ||
|
@@ -1964,7 +1952,7 @@ void SSLWrap<Base>::SetSession(const FunctionCallbackInfo<Value>& args) { | |
return env->ThrowError("Session argument is mandatory"); | ||
} | ||
|
||
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Session"); | ||
THROW_AND_RETURN_IF_NOT_BUFFER(env, args[0], "Session"); | ||
size_t slen = Buffer::Length(args[0]); | ||
char* sbuf = new char[slen]; | ||
memcpy(sbuf, Buffer::Data(args[0]), slen); | ||
|
@@ -2088,7 +2076,7 @@ void SSLWrap<Base>::SetOCSPResponse( | |
if (args.Length() < 1) | ||
return env->ThrowTypeError("OCSP response argument is mandatory"); | ||
|
||
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "OCSP response"); | ||
THROW_AND_RETURN_IF_NOT_BUFFER(env, args[0], "OCSP response"); | ||
|
||
w->ocsp_response_.Reset(args.GetIsolate(), args[0].As<Object>()); | ||
#endif // NODE__HAVE_TLSEXT_STATUS_CB | ||
|
@@ -3937,11 +3925,11 @@ template <PublicKeyCipher::Operation operation, | |
void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
|
||
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Key"); | ||
THROW_AND_RETURN_IF_NOT_BUFFER(env, args[0], "Key"); | ||
char* kbuf = Buffer::Data(args[0]); | ||
ssize_t klen = Buffer::Length(args[0]); | ||
|
||
THROW_AND_RETURN_IF_NOT_BUFFER(args[1], "Data"); | ||
THROW_AND_RETURN_IF_NOT_BUFFER(env, args[1], "Data"); | ||
char* buf = Buffer::Data(args[1]); | ||
ssize_t len = Buffer::Length(args[1]); | ||
|
||
|
@@ -4097,7 +4085,7 @@ void DiffieHellman::DiffieHellmanGroup( | |
return env->ThrowError("Group name argument is mandatory"); | ||
} | ||
|
||
THROW_AND_RETURN_IF_NOT_STRING(args[0], "Group name"); | ||
THROW_AND_RETURN_IF_NOT_STRING(env, args[0], "Group name"); | ||
|
||
bool initialized = false; | ||
|
||
|
@@ -4246,7 +4234,7 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) { | |
if (args.Length() == 0) { | ||
return env->ThrowError("Other party's public key argument is mandatory"); | ||
} else { | ||
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Other party's public key"); | ||
THROW_AND_RETURN_IF_NOT_BUFFER(env, args[0], "Other party's public key"); | ||
key = BN_bin2bn( | ||
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])), | ||
Buffer::Length(args[0]), | ||
|
@@ -4319,7 +4307,7 @@ void DiffieHellman::SetKey(const v8::FunctionCallbackInfo<v8::Value>& args, | |
|
||
if (!Buffer::HasInstance(args[0])) { | ||
snprintf(errmsg, sizeof(errmsg), "%s must be a buffer", what); | ||
return env->ThrowTypeError(errmsg); | ||
return node::THROW_ERR_INVALID_ARG_TYPE(env, errmsg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps remove the |
||
} | ||
|
||
BIGNUM* num = | ||
|
@@ -4397,7 +4385,7 @@ void ECDH::New(const FunctionCallbackInfo<Value>& args) { | |
MarkPopErrorOnReturn mark_pop_error_on_return; | ||
|
||
// TODO(indutny): Support raw curves? | ||
THROW_AND_RETURN_IF_NOT_STRING(args[0], "ECDH curve name"); | ||
THROW_AND_RETURN_IF_NOT_STRING(env, args[0], "ECDH curve name"); | ||
node::Utf8Value curve(env->isolate(), args[0]); | ||
|
||
int nid = OBJ_sn2nid(*curve); | ||
|
@@ -4454,7 +4442,7 @@ EC_POINT* ECDH::BufferToPoint(Environment* env, | |
void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
|
||
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Data"); | ||
THROW_AND_RETURN_IF_NOT_BUFFER(env, args[0], "Data"); | ||
|
||
ECDH* ecdh; | ||
ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder()); | ||
|
@@ -4557,7 +4545,7 @@ void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) { | |
ECDH* ecdh; | ||
ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder()); | ||
|
||
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Private key"); | ||
THROW_AND_RETURN_IF_NOT_BUFFER(env, args[0], "Private key"); | ||
|
||
BIGNUM* priv = BN_bin2bn( | ||
reinterpret_cast<unsigned char*>(Buffer::Data(args[0].As<Object>())), | ||
|
@@ -4611,7 +4599,7 @@ void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) { | |
ECDH* ecdh; | ||
ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder()); | ||
|
||
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Public key"); | ||
THROW_AND_RETURN_IF_NOT_BUFFER(env, args[0], "Public key"); | ||
|
||
MarkPopErrorOnReturn mark_pop_error_on_return; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps remove the
node
namespace here?