mirror of https://github.com/nodejs/node.git
crypto: fix -Wtautological-compare warning
parent
be5a8e24c2
commit
eec8c2edaf
|
@ -4346,8 +4346,6 @@ err:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
typedef int (*RandomBytesGenerator)(unsigned char* buf, int size);
|
|
||||||
|
|
||||||
struct RandomBytesRequest {
|
struct RandomBytesRequest {
|
||||||
~RandomBytesRequest();
|
~RandomBytesRequest();
|
||||||
Persistent<Object> obj_;
|
Persistent<Object> obj_;
|
||||||
|
@ -4370,26 +4368,26 @@ void RandomBytesFree(char* data, void* hint) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <RandomBytesGenerator generator>
|
template <bool pseudoRandom>
|
||||||
void RandomBytesWork(uv_work_t* work_req) {
|
void RandomBytesWork(uv_work_t* work_req) {
|
||||||
RandomBytesRequest* req =
|
RandomBytesRequest* req = container_of(work_req,
|
||||||
container_of(work_req, RandomBytesRequest, work_req_);
|
RandomBytesRequest,
|
||||||
|
work_req_);
|
||||||
|
int r;
|
||||||
|
|
||||||
int r = generator(reinterpret_cast<unsigned char*>(req->data_), req->size_);
|
if (pseudoRandom == true) {
|
||||||
|
r = RAND_pseudo_bytes(reinterpret_cast<unsigned char*>(req->data_),
|
||||||
|
req->size_);
|
||||||
|
} else {
|
||||||
|
r = RAND_bytes(reinterpret_cast<unsigned char*>(req->data_), req->size_);
|
||||||
|
}
|
||||||
|
|
||||||
switch (r) {
|
// RAND_bytes() returns 0 on error. RAND_pseudo_bytes() returns 0 when the
|
||||||
case 0:
|
// result is not cryptographically strong - but that's not an error.
|
||||||
// RAND_bytes() returns 0 on error, RAND_pseudo_bytes() returns 0
|
if (r == 0 && pseudoRandom == false) {
|
||||||
// when the result is not cryptographically strong - the latter
|
|
||||||
// sucks but is not an error
|
|
||||||
if (generator == RAND_bytes)
|
|
||||||
req->error_ = ERR_get_error();
|
req->error_ = ERR_get_error();
|
||||||
break;
|
} else if (r == -1) {
|
||||||
|
req->error_ = static_cast<unsigned long>(-1);
|
||||||
case -1:
|
|
||||||
// not supported - can this actually happen?
|
|
||||||
req->error_ = (unsigned long) -1;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4414,10 +4412,10 @@ void RandomBytesCheck(RandomBytesRequest* req, Local<Value> argv[2]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <RandomBytesGenerator generator>
|
|
||||||
void RandomBytesAfter(uv_work_t* work_req) {
|
void RandomBytesAfter(uv_work_t* work_req) {
|
||||||
RandomBytesRequest* req =
|
RandomBytesRequest* req = container_of(work_req,
|
||||||
container_of(work_req, RandomBytesRequest, work_req_);
|
RandomBytesRequest,
|
||||||
|
work_req_);
|
||||||
|
|
||||||
HandleScope scope;
|
HandleScope scope;
|
||||||
Local<Value> argv[2];
|
Local<Value> argv[2];
|
||||||
|
@ -4428,7 +4426,7 @@ void RandomBytesAfter(uv_work_t* work_req) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <RandomBytesGenerator generator>
|
template <bool pseudoRandom>
|
||||||
Handle<Value> RandomBytes(const Arguments& args) {
|
Handle<Value> RandomBytes(const Arguments& args) {
|
||||||
HandleScope scope;
|
HandleScope scope;
|
||||||
|
|
||||||
|
@ -4452,14 +4450,14 @@ Handle<Value> RandomBytes(const Arguments& args) {
|
||||||
|
|
||||||
uv_queue_work(uv_default_loop(),
|
uv_queue_work(uv_default_loop(),
|
||||||
&req->work_req_,
|
&req->work_req_,
|
||||||
RandomBytesWork<generator>,
|
RandomBytesWork<pseudoRandom>,
|
||||||
RandomBytesAfter<generator>);
|
RandomBytesAfter);
|
||||||
|
|
||||||
return req->obj_;
|
return req->obj_;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Local<Value> argv[2];
|
Local<Value> argv[2];
|
||||||
RandomBytesWork<generator>(&req->work_req_);
|
RandomBytesWork<pseudoRandom>(&req->work_req_);
|
||||||
RandomBytesCheck(req, argv);
|
RandomBytesCheck(req, argv);
|
||||||
delete req;
|
delete req;
|
||||||
|
|
||||||
|
@ -4508,8 +4506,8 @@ void InitCrypto(Handle<Object> target) {
|
||||||
Verify::Initialize(target);
|
Verify::Initialize(target);
|
||||||
|
|
||||||
NODE_SET_METHOD(target, "PBKDF2", PBKDF2);
|
NODE_SET_METHOD(target, "PBKDF2", PBKDF2);
|
||||||
NODE_SET_METHOD(target, "randomBytes", RandomBytes<RAND_bytes>);
|
NODE_SET_METHOD(target, "randomBytes", RandomBytes<false>);
|
||||||
NODE_SET_METHOD(target, "pseudoRandomBytes", RandomBytes<RAND_pseudo_bytes>);
|
NODE_SET_METHOD(target, "pseudoRandomBytes", RandomBytes<true>);
|
||||||
|
|
||||||
subject_symbol = NODE_PSYMBOL("subject");
|
subject_symbol = NODE_PSYMBOL("subject");
|
||||||
issuer_symbol = NODE_PSYMBOL("issuer");
|
issuer_symbol = NODE_PSYMBOL("issuer");
|
||||||
|
|
Loading…
Reference in New Issue