Skip to content
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: add ASSERT_EQ style macros #529

Merged
merged 3 commits into from
Jan 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 32 additions & 29 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
#define OPENSSL_CONST
#endif

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

#define ASSERT_IS_BUFFER(val) do { \
#define THROW_AND_RETURN_IF_NOT_BUFFER(val) \
do { \
if (!Buffer::HasInstance(val)) { \
return env->ThrowTypeError("Not a buffer"); \
} \
Expand Down Expand Up @@ -834,7 +836,7 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
}

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

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

ASSERT_IS_BUFFER(args[1]);
ASSERT_IS_BUFFER(args[2]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[2]);

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

ASSERT_IS_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);

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

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

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

ASSERT_IS_STRING_OR_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);

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

ASSERT_IS_BUFFER(args[1]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);

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

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

ASSERT_IS_STRING_OR_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);

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

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

ASSERT_IS_STRING_OR_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);

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

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

ASSERT_IS_STRING_OR_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);

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

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

ASSERT_IS_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
size_t buf_len = Buffer::Length(args[0]);
char* buf = Buffer::Data(args[0]);

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

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

ASSERT_IS_STRING_OR_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);

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

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

ASSERT_IS_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
char* kbuf = Buffer::Data(args[0]);
ssize_t klen = Buffer::Length(args[0]);

ASSERT_IS_STRING_OR_BUFFER(args[1]);
THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[1]);

// BINARY works for both buffers and binary strings.
enum encoding encoding = BINARY;
if (args.Length() >= 3) {
Expand Down Expand Up @@ -3628,11 +3631,11 @@ template <PublicKeyCipher::Operation operation,
void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

ASSERT_IS_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
char* kbuf = Buffer::Data(args[0]);
ssize_t klen = Buffer::Length(args[0]);

ASSERT_IS_BUFFER(args[1]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
char* buf = Buffer::Data(args[1]);
ssize_t len = Buffer::Length(args[1]);

Expand Down Expand Up @@ -3939,7 +3942,7 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
if (args.Length() == 0) {
return env->ThrowError("First argument must be other party's public key");
} else {
ASSERT_IS_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
key = BN_bin2bn(
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
Buffer::Length(args[0]),
Expand Down Expand Up @@ -4005,7 +4008,7 @@ void DiffieHellman::SetPublicKey(const FunctionCallbackInfo<Value>& args) {
if (args.Length() == 0) {
return env->ThrowError("First argument must be public key");
} else {
ASSERT_IS_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
diffieHellman->dh->pub_key = BN_bin2bn(
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
Buffer::Length(args[0]), 0);
Expand All @@ -4024,7 +4027,7 @@ void DiffieHellman::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
if (args.Length() == 0) {
return env->ThrowError("First argument must be private key");
} else {
ASSERT_IS_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
diffieHellman->dh->priv_key = BN_bin2bn(
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
Buffer::Length(args[0]),
Expand Down Expand Up @@ -4137,7 +4140,7 @@ EC_POINT* ECDH::BufferToPoint(char* data, size_t len) {
void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

ASSERT_IS_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);

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

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

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

ASSERT_IS_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);

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

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

ASSERT_IS_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);

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

ASSERT_IS_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
passlen = Buffer::Length(args[0]);
if (passlen < 0) {
type_error = "Bad password";
goto err;
}

ASSERT_IS_BUFFER(args[1]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);

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

ASSERT_IS_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);

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

ASSERT_IS_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);

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

ASSERT_IS_BUFFER(args[0]);
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);

size_t len = Buffer::Length(args[0]);
if (len == 0)
Expand Down
2 changes: 1 addition & 1 deletion src/node_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ class ECDH : public BaseObject {
key_(key),
group_(EC_KEY_get0_group(key_)) {
MakeWeak<ECDH>(this);
ASSERT(group_ != nullptr);
ASSERT_NE(group_, nullptr);
}

static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down
26 changes: 13 additions & 13 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ using v8::Value;

#define THROW_BAD_ARGS TYPE_ERROR("Bad argument")

#define GET_OFFSET(a) ((a)->IsNumber() ? (a)->IntegerValue() : -1)

class FSReqWrap: public ReqWrap<uv_fs_t> {
public:
void* operator new(size_t size) { return new char[size]; }
Expand Down Expand Up @@ -85,17 +87,6 @@ static void NewFSReqWrap(const FunctionCallbackInfo<Value>& args) {
}


#define ASSERT_OFFSET(a) \
if (!(a)->IsUndefined() && !(a)->IsNull() && !IsInt64((a)->NumberValue())) { \
return env->ThrowTypeError("Not an integer"); \
}
#define ASSERT_TRUNCATE_LENGTH(a) \
if (!(a)->IsUndefined() && !(a)->IsNull() && !IsInt64((a)->NumberValue())) { \
return env->ThrowTypeError("Not an integer"); \
}
#define GET_OFFSET(a) ((a)->IsNumber() ? (a)->IntegerValue() : -1)
#define GET_TRUNCATE_LENGTH(a) ((a)->IntegerValue())

static inline bool IsInt64(double x) {
return x == static_cast<double>(static_cast<int64_t>(x));
}
Expand Down Expand Up @@ -599,8 +590,17 @@ static void FTruncate(const FunctionCallbackInfo<Value>& args) {

int fd = args[0]->Int32Value();

ASSERT_TRUNCATE_LENGTH(args[1]);
int64_t len = GET_TRUNCATE_LENGTH(args[1]);
// FIXME(bnoordhuis) It's questionable to reject non-ints here but still
// allow implicit coercion from null or undefined to zero. Probably best
// handled in lib/fs.js.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. it's much easier to handle value coercion from JS. even if that means all there is is a minor wrapper that does a couple coercions and checks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, I'll address that in a follow-up PR.

Local<Value> len_v(args[1]);
if (!len_v->IsUndefined() &&
!len_v->IsNull() &&
!IsInt64(len_v->NumberValue())) {
return env->ThrowTypeError("Not an integer");
}

const int64_t len = len_v->IntegerValue();

if (args[2]->IsObject()) {
ASYNC_CALL(ftruncate, args[2], fd, len)
Expand Down
7 changes: 7 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ namespace node {
# define CHECK(expression) assert(expression)
#endif

#define ASSERT_EQ(a, b) ASSERT((a) == (b))
#define ASSERT_GE(a, b) ASSERT((a) >= (b))
#define ASSERT_GT(a, b) ASSERT((a) > (b))
#define ASSERT_LE(a, b) ASSERT((a) <= (b))
#define ASSERT_LT(a, b) ASSERT((a) < (b))
#define ASSERT_NE(a, b) ASSERT((a) != (b))

#define CHECK_EQ(a, b) CHECK((a) == (b))
#define CHECK_GE(a, b) CHECK((a) >= (b))
#define CHECK_GT(a, b) CHECK((a) > (b))
Expand Down