Permalink
Browse files

crypto: fix -Wtautological-compare warning

  • Loading branch information...
1 parent be5a8e2 commit eec8c2edafdb52a4c63dfab77a8d84293dc458d3 @bnoordhuis bnoordhuis committed Oct 7, 2012
Showing with 27 additions and 29 deletions.
  1. +27 −29 src/node_crypto.cc
View
@@ -4346,8 +4346,6 @@ PBKDF2(const Arguments& args) {
}
-typedef int (*RandomBytesGenerator)(unsigned char* buf, int size);
-
struct RandomBytesRequest {
~RandomBytesRequest();
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) {
- RandomBytesRequest* req =
- container_of(work_req, RandomBytesRequest, work_req_);
-
- int r = generator(reinterpret_cast<unsigned char*>(req->data_), req->size_);
-
- switch (r) {
- case 0:
- // RAND_bytes() returns 0 on error, RAND_pseudo_bytes() returns 0
- // when the result is not cryptographically strong - the latter
- // sucks but is not an error
- if (generator == RAND_bytes)
- req->error_ = ERR_get_error();
- break;
+ RandomBytesRequest* req = container_of(work_req,
+ RandomBytesRequest,
+ work_req_);
+ int r;
+
+ 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_);
+ }
- case -1:
- // not supported - can this actually happen?
- req->error_ = (unsigned long) -1;
- break;
+ // RAND_bytes() returns 0 on error. RAND_pseudo_bytes() returns 0 when the
+ // result is not cryptographically strong - but that's not an error.
+ if (r == 0 && pseudoRandom == false) {
+ req->error_ = ERR_get_error();
+ } else if (r == -1) {
+ req->error_ = static_cast<unsigned long>(-1);
}
}
@@ -4414,10 +4412,10 @@ void RandomBytesCheck(RandomBytesRequest* req, Local<Value> argv[2]) {
}
-template <RandomBytesGenerator generator>
void RandomBytesAfter(uv_work_t* work_req) {
- RandomBytesRequest* req =
- container_of(work_req, RandomBytesRequest, work_req_);
+ RandomBytesRequest* req = container_of(work_req,
+ RandomBytesRequest,
+ work_req_);
HandleScope scope;
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) {
HandleScope scope;
@@ -4452,14 +4450,14 @@ Handle<Value> RandomBytes(const Arguments& args) {
uv_queue_work(uv_default_loop(),
&req->work_req_,
- RandomBytesWork<generator>,
- RandomBytesAfter<generator>);
+ RandomBytesWork<pseudoRandom>,
+ RandomBytesAfter);
return req->obj_;
}
else {
Local<Value> argv[2];
- RandomBytesWork<generator>(&req->work_req_);
+ RandomBytesWork<pseudoRandom>(&req->work_req_);
RandomBytesCheck(req, argv);
delete req;
@@ -4508,8 +4506,8 @@ void InitCrypto(Handle<Object> target) {
Verify::Initialize(target);
NODE_SET_METHOD(target, "PBKDF2", PBKDF2);
- NODE_SET_METHOD(target, "randomBytes", RandomBytes<RAND_bytes>);
- NODE_SET_METHOD(target, "pseudoRandomBytes", RandomBytes<RAND_pseudo_bytes>);
+ NODE_SET_METHOD(target, "randomBytes", RandomBytes<false>);
+ NODE_SET_METHOD(target, "pseudoRandomBytes", RandomBytes<true>);
subject_symbol = NODE_PSYMBOL("subject");
issuer_symbol = NODE_PSYMBOL("issuer");

0 comments on commit eec8c2e

Please sign in to comment.