28
28
#define OPENSSL_CONST
29
29
#endif
30
30
31
- #define ASSERT_IS_STRING_OR_BUFFER (val ) do { \
31
+ #define THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER (val ) \
32
+ do { \
32
33
if (!Buffer::HasInstance (val) && !val->IsString ()) { \
33
34
return env->ThrowTypeError (" Not a string or buffer" ); \
34
35
} \
35
36
} while (0 )
36
37
37
- #define ASSERT_IS_BUFFER (val ) do { \
38
+ #define THROW_AND_RETURN_IF_NOT_BUFFER (val ) \
39
+ do { \
38
40
if (!Buffer::HasInstance (val)) { \
39
41
return env->ThrowTypeError (" Not a buffer" ); \
40
42
} \
@@ -834,7 +836,7 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
834
836
}
835
837
836
838
if (args.Length () >= 2 ) {
837
- ASSERT_IS_BUFFER (args[1 ]);
839
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[1 ]);
838
840
size_t passlen = Buffer::Length (args[1 ]);
839
841
pass = new char [passlen + 1 ];
840
842
memcpy (pass, Buffer::Data (args[1 ]), passlen);
@@ -1432,7 +1434,7 @@ void SSLWrap<Base>::SetSession(const FunctionCallbackInfo<Value>& args) {
1432
1434
return env->ThrowTypeError (" Bad argument" );
1433
1435
}
1434
1436
1435
- ASSERT_IS_BUFFER (args[0 ]);
1437
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
1436
1438
size_t slen = Buffer::Length (args[0 ]);
1437
1439
char * sbuf = new char [slen];
1438
1440
memcpy (sbuf, Buffer::Data (args[0 ]), slen);
@@ -2608,8 +2610,8 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) {
2608
2610
return env->ThrowError (" Must give cipher-type, key, and iv as argument" );
2609
2611
}
2610
2612
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 ]);
2613
2615
2614
2616
const node::Utf8Value cipher_type (env->isolate (), args[0 ]);
2615
2617
ssize_t key_len = Buffer::Length (args[1 ]);
@@ -2699,7 +2701,7 @@ bool CipherBase::SetAAD(const char* data, unsigned int len) {
2699
2701
void CipherBase::SetAAD (const FunctionCallbackInfo<Value>& args) {
2700
2702
Environment* env = Environment::GetCurrent (args);
2701
2703
2702
- ASSERT_IS_BUFFER (args[0 ]);
2704
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
2703
2705
2704
2706
CipherBase* cipher = Unwrap<CipherBase>(args.Holder ());
2705
2707
@@ -2740,7 +2742,7 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
2740
2742
2741
2743
CipherBase* cipher = Unwrap<CipherBase>(args.Holder ());
2742
2744
2743
- ASSERT_IS_STRING_OR_BUFFER (args[0 ]);
2745
+ THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER (args[0 ]);
2744
2746
2745
2747
unsigned char * out = nullptr ;
2746
2748
bool r;
@@ -2900,7 +2902,7 @@ void Hmac::HmacInit(const FunctionCallbackInfo<Value>& args) {
2900
2902
return env->ThrowError (" Must give hashtype string, key as arguments" );
2901
2903
}
2902
2904
2903
- ASSERT_IS_BUFFER (args[1 ]);
2905
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[1 ]);
2904
2906
2905
2907
const node::Utf8Value hash_type (env->isolate (), args[0 ]);
2906
2908
const char * buffer_data = Buffer::Data (args[1 ]);
@@ -2922,7 +2924,7 @@ void Hmac::HmacUpdate(const FunctionCallbackInfo<Value>& args) {
2922
2924
2923
2925
Hmac* hmac = Unwrap<Hmac>(args.Holder ());
2924
2926
2925
- ASSERT_IS_STRING_OR_BUFFER (args[0 ]);
2927
+ THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER (args[0 ]);
2926
2928
2927
2929
// Only copy the data if we have to, because it's a string
2928
2930
bool r;
@@ -3046,7 +3048,7 @@ void Hash::HashUpdate(const FunctionCallbackInfo<Value>& args) {
3046
3048
3047
3049
Hash* hash = Unwrap<Hash>(args.Holder ());
3048
3050
3049
- ASSERT_IS_STRING_OR_BUFFER (args[0 ]);
3051
+ THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER (args[0 ]);
3050
3052
3051
3053
// Only copy the data if we have to, because it's a string
3052
3054
bool r;
@@ -3207,7 +3209,7 @@ void Sign::SignUpdate(const FunctionCallbackInfo<Value>& args) {
3207
3209
3208
3210
Sign* sign = Unwrap<Sign>(args.Holder ());
3209
3211
3210
- ASSERT_IS_STRING_OR_BUFFER (args[0 ]);
3212
+ THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER (args[0 ]);
3211
3213
3212
3214
// Only copy the data if we have to, because it's a string
3213
3215
Error err;
@@ -3296,7 +3298,7 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
3296
3298
3297
3299
node::Utf8Value passphrase (env->isolate (), args[2 ]);
3298
3300
3299
- ASSERT_IS_BUFFER (args[0 ]);
3301
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
3300
3302
size_t buf_len = Buffer::Length (args[0 ]);
3301
3303
char * buf = Buffer::Data (args[0 ]);
3302
3304
@@ -3388,7 +3390,7 @@ void Verify::VerifyUpdate(const FunctionCallbackInfo<Value>& args) {
3388
3390
3389
3391
Verify* verify = Unwrap<Verify>(args.Holder ());
3390
3392
3391
- ASSERT_IS_STRING_OR_BUFFER (args[0 ]);
3393
+ THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER (args[0 ]);
3392
3394
3393
3395
// Only copy the data if we have to, because it's a string
3394
3396
Error err;
@@ -3496,11 +3498,12 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
3496
3498
3497
3499
Verify* verify = Unwrap<Verify>(args.Holder ());
3498
3500
3499
- ASSERT_IS_BUFFER (args[0 ]);
3501
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
3500
3502
char * kbuf = Buffer::Data (args[0 ]);
3501
3503
ssize_t klen = Buffer::Length (args[0 ]);
3502
3504
3503
- ASSERT_IS_STRING_OR_BUFFER (args[1 ]);
3505
+ THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER (args[1 ]);
3506
+
3504
3507
// BINARY works for both buffers and binary strings.
3505
3508
enum encoding encoding = BINARY;
3506
3509
if (args.Length () >= 3 ) {
@@ -3628,11 +3631,11 @@ template <PublicKeyCipher::Operation operation,
3628
3631
void PublicKeyCipher::Cipher (const FunctionCallbackInfo<Value>& args) {
3629
3632
Environment* env = Environment::GetCurrent (args);
3630
3633
3631
- ASSERT_IS_BUFFER (args[0 ]);
3634
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
3632
3635
char * kbuf = Buffer::Data (args[0 ]);
3633
3636
ssize_t klen = Buffer::Length (args[0 ]);
3634
3637
3635
- ASSERT_IS_BUFFER (args[1 ]);
3638
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[1 ]);
3636
3639
char * buf = Buffer::Data (args[1 ]);
3637
3640
ssize_t len = Buffer::Length (args[1 ]);
3638
3641
@@ -3939,7 +3942,7 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
3939
3942
if (args.Length () == 0 ) {
3940
3943
return env->ThrowError (" First argument must be other party's public key" );
3941
3944
} else {
3942
- ASSERT_IS_BUFFER (args[0 ]);
3945
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
3943
3946
key = BN_bin2bn (
3944
3947
reinterpret_cast <unsigned char *>(Buffer::Data (args[0 ])),
3945
3948
Buffer::Length (args[0 ]),
@@ -4005,7 +4008,7 @@ void DiffieHellman::SetPublicKey(const FunctionCallbackInfo<Value>& args) {
4005
4008
if (args.Length () == 0 ) {
4006
4009
return env->ThrowError (" First argument must be public key" );
4007
4010
} else {
4008
- ASSERT_IS_BUFFER (args[0 ]);
4011
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
4009
4012
diffieHellman->dh ->pub_key = BN_bin2bn (
4010
4013
reinterpret_cast <unsigned char *>(Buffer::Data (args[0 ])),
4011
4014
Buffer::Length (args[0 ]), 0 );
@@ -4024,7 +4027,7 @@ void DiffieHellman::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
4024
4027
if (args.Length () == 0 ) {
4025
4028
return env->ThrowError (" First argument must be private key" );
4026
4029
} else {
4027
- ASSERT_IS_BUFFER (args[0 ]);
4030
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
4028
4031
diffieHellman->dh ->priv_key = BN_bin2bn (
4029
4032
reinterpret_cast <unsigned char *>(Buffer::Data (args[0 ])),
4030
4033
Buffer::Length (args[0 ]),
@@ -4137,7 +4140,7 @@ EC_POINT* ECDH::BufferToPoint(char* data, size_t len) {
4137
4140
void ECDH::ComputeSecret (const FunctionCallbackInfo<Value>& args) {
4138
4141
Environment* env = Environment::GetCurrent (args);
4139
4142
4140
- ASSERT_IS_BUFFER (args[0 ]);
4143
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
4141
4144
4142
4145
ECDH* ecdh = Unwrap<ECDH>(args.Holder ());
4143
4146
@@ -4233,7 +4236,7 @@ void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
4233
4236
4234
4237
ECDH* ecdh = Unwrap<ECDH>(args.Holder ());
4235
4238
4236
- ASSERT_IS_BUFFER (args[0 ]);
4239
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
4237
4240
4238
4241
BIGNUM* priv = BN_bin2bn (
4239
4242
reinterpret_cast <unsigned char *>(Buffer::Data (args[0 ].As <Object>())),
@@ -4252,7 +4255,7 @@ void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) {
4252
4255
4253
4256
ECDH* ecdh = Unwrap<ECDH>(args.Holder ());
4254
4257
4255
- ASSERT_IS_BUFFER (args[0 ]);
4258
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
4256
4259
4257
4260
EC_POINT* pub = ecdh->BufferToPoint (Buffer::Data (args[0 ].As <Object>()),
4258
4261
Buffer::Length (args[0 ].As <Object>()));
@@ -4429,14 +4432,14 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
4429
4432
goto err;
4430
4433
}
4431
4434
4432
- ASSERT_IS_BUFFER (args[0 ]);
4435
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
4433
4436
passlen = Buffer::Length (args[0 ]);
4434
4437
if (passlen < 0 ) {
4435
4438
type_error = " Bad password" ;
4436
4439
goto err;
4437
4440
}
4438
4441
4439
- ASSERT_IS_BUFFER (args[1 ]);
4442
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[1 ]);
4440
4443
4441
4444
pass = static_cast <char *>(malloc (passlen));
4442
4445
if (pass == nullptr ) {
@@ -4816,7 +4819,7 @@ void Certificate::VerifySpkac(const FunctionCallbackInfo<Value>& args) {
4816
4819
if (args.Length () < 1 )
4817
4820
return env->ThrowTypeError (" Missing argument" );
4818
4821
4819
- ASSERT_IS_BUFFER (args[0 ]);
4822
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
4820
4823
4821
4824
size_t length = Buffer::Length (args[0 ]);
4822
4825
if (length == 0 )
@@ -4880,7 +4883,7 @@ void Certificate::ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
4880
4883
if (args.Length () < 1 )
4881
4884
return env->ThrowTypeError (" Missing argument" );
4882
4885
4883
- ASSERT_IS_BUFFER (args[0 ]);
4886
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
4884
4887
4885
4888
size_t length = Buffer::Length (args[0 ]);
4886
4889
if (length == 0 )
@@ -4923,7 +4926,7 @@ void Certificate::ExportChallenge(const FunctionCallbackInfo<Value>& args) {
4923
4926
if (args.Length () < 1 )
4924
4927
return env->ThrowTypeError (" Missing argument" );
4925
4928
4926
- ASSERT_IS_BUFFER (args[0 ]);
4929
+ THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ]);
4927
4930
4928
4931
size_t len = Buffer::Length (args[0 ]);
4929
4932
if (len == 0 )
0 commit comments