Skip to content

Commit 52f624e

Browse files
committed
src: rename ASSERT macros in node_crypto.cc
Rename the misnomers ASSERT_IS_STRING_OR_BUFFER and ASSERT_IS_BUFFER. Said macros don't assert, they throw a TypeError and return. PR-URL: #529 Reviewed-By: Trevor Norris <[email protected]>
1 parent e95cfe1 commit 52f624e

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/node_crypto.cc

+32-29
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
#define OPENSSL_CONST
2929
#endif
3030

31-
#define ASSERT_IS_STRING_OR_BUFFER(val) do { \
31+
#define THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(val) \
32+
do { \
3233
if (!Buffer::HasInstance(val) && !val->IsString()) { \
3334
return env->ThrowTypeError("Not a string or buffer"); \
3435
} \
3536
} while (0)
3637

37-
#define ASSERT_IS_BUFFER(val) do { \
38+
#define THROW_AND_RETURN_IF_NOT_BUFFER(val) \
39+
do { \
3840
if (!Buffer::HasInstance(val)) { \
3941
return env->ThrowTypeError("Not a buffer"); \
4042
} \
@@ -834,7 +836,7 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
834836
}
835837

836838
if (args.Length() >= 2) {
837-
ASSERT_IS_BUFFER(args[1]);
839+
THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
838840
size_t passlen = Buffer::Length(args[1]);
839841
pass = new char[passlen + 1];
840842
memcpy(pass, Buffer::Data(args[1]), passlen);
@@ -1432,7 +1434,7 @@ void SSLWrap<Base>::SetSession(const FunctionCallbackInfo<Value>& args) {
14321434
return env->ThrowTypeError("Bad argument");
14331435
}
14341436

1435-
ASSERT_IS_BUFFER(args[0]);
1437+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
14361438
size_t slen = Buffer::Length(args[0]);
14371439
char* sbuf = new char[slen];
14381440
memcpy(sbuf, Buffer::Data(args[0]), slen);
@@ -2608,8 +2610,8 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) {
26082610
return env->ThrowError("Must give cipher-type, key, and iv as argument");
26092611
}
26102612

2611-
ASSERT_IS_BUFFER(args[1]);
2612-
ASSERT_IS_BUFFER(args[2]);
2613+
THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
2614+
THROW_AND_RETURN_IF_NOT_BUFFER(args[2]);
26132615

26142616
const node::Utf8Value cipher_type(env->isolate(), args[0]);
26152617
ssize_t key_len = Buffer::Length(args[1]);
@@ -2699,7 +2701,7 @@ bool CipherBase::SetAAD(const char* data, unsigned int len) {
26992701
void CipherBase::SetAAD(const FunctionCallbackInfo<Value>& args) {
27002702
Environment* env = Environment::GetCurrent(args);
27012703

2702-
ASSERT_IS_BUFFER(args[0]);
2704+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
27032705

27042706
CipherBase* cipher = Unwrap<CipherBase>(args.Holder());
27052707

@@ -2740,7 +2742,7 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
27402742

27412743
CipherBase* cipher = Unwrap<CipherBase>(args.Holder());
27422744

2743-
ASSERT_IS_STRING_OR_BUFFER(args[0]);
2745+
THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
27442746

27452747
unsigned char* out = nullptr;
27462748
bool r;
@@ -2900,7 +2902,7 @@ void Hmac::HmacInit(const FunctionCallbackInfo<Value>& args) {
29002902
return env->ThrowError("Must give hashtype string, key as arguments");
29012903
}
29022904

2903-
ASSERT_IS_BUFFER(args[1]);
2905+
THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
29042906

29052907
const node::Utf8Value hash_type(env->isolate(), args[0]);
29062908
const char* buffer_data = Buffer::Data(args[1]);
@@ -2922,7 +2924,7 @@ void Hmac::HmacUpdate(const FunctionCallbackInfo<Value>& args) {
29222924

29232925
Hmac* hmac = Unwrap<Hmac>(args.Holder());
29242926

2925-
ASSERT_IS_STRING_OR_BUFFER(args[0]);
2927+
THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
29262928

29272929
// Only copy the data if we have to, because it's a string
29282930
bool r;
@@ -3046,7 +3048,7 @@ void Hash::HashUpdate(const FunctionCallbackInfo<Value>& args) {
30463048

30473049
Hash* hash = Unwrap<Hash>(args.Holder());
30483050

3049-
ASSERT_IS_STRING_OR_BUFFER(args[0]);
3051+
THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
30503052

30513053
// Only copy the data if we have to, because it's a string
30523054
bool r;
@@ -3207,7 +3209,7 @@ void Sign::SignUpdate(const FunctionCallbackInfo<Value>& args) {
32073209

32083210
Sign* sign = Unwrap<Sign>(args.Holder());
32093211

3210-
ASSERT_IS_STRING_OR_BUFFER(args[0]);
3212+
THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
32113213

32123214
// Only copy the data if we have to, because it's a string
32133215
Error err;
@@ -3296,7 +3298,7 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
32963298

32973299
node::Utf8Value passphrase(env->isolate(), args[2]);
32983300

3299-
ASSERT_IS_BUFFER(args[0]);
3301+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
33003302
size_t buf_len = Buffer::Length(args[0]);
33013303
char* buf = Buffer::Data(args[0]);
33023304

@@ -3388,7 +3390,7 @@ void Verify::VerifyUpdate(const FunctionCallbackInfo<Value>& args) {
33883390

33893391
Verify* verify = Unwrap<Verify>(args.Holder());
33903392

3391-
ASSERT_IS_STRING_OR_BUFFER(args[0]);
3393+
THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
33923394

33933395
// Only copy the data if we have to, because it's a string
33943396
Error err;
@@ -3496,11 +3498,12 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
34963498

34973499
Verify* verify = Unwrap<Verify>(args.Holder());
34983500

3499-
ASSERT_IS_BUFFER(args[0]);
3501+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
35003502
char* kbuf = Buffer::Data(args[0]);
35013503
ssize_t klen = Buffer::Length(args[0]);
35023504

3503-
ASSERT_IS_STRING_OR_BUFFER(args[1]);
3505+
THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[1]);
3506+
35043507
// BINARY works for both buffers and binary strings.
35053508
enum encoding encoding = BINARY;
35063509
if (args.Length() >= 3) {
@@ -3628,11 +3631,11 @@ template <PublicKeyCipher::Operation operation,
36283631
void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
36293632
Environment* env = Environment::GetCurrent(args);
36303633

3631-
ASSERT_IS_BUFFER(args[0]);
3634+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
36323635
char* kbuf = Buffer::Data(args[0]);
36333636
ssize_t klen = Buffer::Length(args[0]);
36343637

3635-
ASSERT_IS_BUFFER(args[1]);
3638+
THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
36363639
char* buf = Buffer::Data(args[1]);
36373640
ssize_t len = Buffer::Length(args[1]);
36383641

@@ -3939,7 +3942,7 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
39393942
if (args.Length() == 0) {
39403943
return env->ThrowError("First argument must be other party's public key");
39413944
} else {
3942-
ASSERT_IS_BUFFER(args[0]);
3945+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
39433946
key = BN_bin2bn(
39443947
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
39453948
Buffer::Length(args[0]),
@@ -4005,7 +4008,7 @@ void DiffieHellman::SetPublicKey(const FunctionCallbackInfo<Value>& args) {
40054008
if (args.Length() == 0) {
40064009
return env->ThrowError("First argument must be public key");
40074010
} else {
4008-
ASSERT_IS_BUFFER(args[0]);
4011+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
40094012
diffieHellman->dh->pub_key = BN_bin2bn(
40104013
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
40114014
Buffer::Length(args[0]), 0);
@@ -4024,7 +4027,7 @@ void DiffieHellman::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
40244027
if (args.Length() == 0) {
40254028
return env->ThrowError("First argument must be private key");
40264029
} else {
4027-
ASSERT_IS_BUFFER(args[0]);
4030+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
40284031
diffieHellman->dh->priv_key = BN_bin2bn(
40294032
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
40304033
Buffer::Length(args[0]),
@@ -4137,7 +4140,7 @@ EC_POINT* ECDH::BufferToPoint(char* data, size_t len) {
41374140
void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
41384141
Environment* env = Environment::GetCurrent(args);
41394142

4140-
ASSERT_IS_BUFFER(args[0]);
4143+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
41414144

41424145
ECDH* ecdh = Unwrap<ECDH>(args.Holder());
41434146

@@ -4233,7 +4236,7 @@ void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
42334236

42344237
ECDH* ecdh = Unwrap<ECDH>(args.Holder());
42354238

4236-
ASSERT_IS_BUFFER(args[0]);
4239+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
42374240

42384241
BIGNUM* priv = BN_bin2bn(
42394242
reinterpret_cast<unsigned char*>(Buffer::Data(args[0].As<Object>())),
@@ -4252,7 +4255,7 @@ void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) {
42524255

42534256
ECDH* ecdh = Unwrap<ECDH>(args.Holder());
42544257

4255-
ASSERT_IS_BUFFER(args[0]);
4258+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
42564259

42574260
EC_POINT* pub = ecdh->BufferToPoint(Buffer::Data(args[0].As<Object>()),
42584261
Buffer::Length(args[0].As<Object>()));
@@ -4429,14 +4432,14 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
44294432
goto err;
44304433
}
44314434

4432-
ASSERT_IS_BUFFER(args[0]);
4435+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
44334436
passlen = Buffer::Length(args[0]);
44344437
if (passlen < 0) {
44354438
type_error = "Bad password";
44364439
goto err;
44374440
}
44384441

4439-
ASSERT_IS_BUFFER(args[1]);
4442+
THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
44404443

44414444
pass = static_cast<char*>(malloc(passlen));
44424445
if (pass == nullptr) {
@@ -4816,7 +4819,7 @@ void Certificate::VerifySpkac(const FunctionCallbackInfo<Value>& args) {
48164819
if (args.Length() < 1)
48174820
return env->ThrowTypeError("Missing argument");
48184821

4819-
ASSERT_IS_BUFFER(args[0]);
4822+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
48204823

48214824
size_t length = Buffer::Length(args[0]);
48224825
if (length == 0)
@@ -4880,7 +4883,7 @@ void Certificate::ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
48804883
if (args.Length() < 1)
48814884
return env->ThrowTypeError("Missing argument");
48824885

4883-
ASSERT_IS_BUFFER(args[0]);
4886+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
48844887

48854888
size_t length = Buffer::Length(args[0]);
48864889
if (length == 0)
@@ -4923,7 +4926,7 @@ void Certificate::ExportChallenge(const FunctionCallbackInfo<Value>& args) {
49234926
if (args.Length() < 1)
49244927
return env->ThrowTypeError("Missing argument");
49254928

4926-
ASSERT_IS_BUFFER(args[0]);
4929+
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
49274930

49284931
size_t len = Buffer::Length(args[0]);
49294932
if (len == 0)

0 commit comments

Comments
 (0)