From eec8c2edafdb52a4c63dfab77a8d84293dc458d3 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 8 Oct 2012 00:00:30 +0200 Subject: [PATCH] crypto: fix -Wtautological-compare warning --- src/node_crypto.cc | 54 ++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 0d681bb81f2..04954328dfc 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -4346,8 +4346,6 @@ err: } -typedef int (*RandomBytesGenerator)(unsigned char* buf, int size); - struct RandomBytesRequest { ~RandomBytesRequest(); Persistent obj_; @@ -4370,26 +4368,26 @@ void RandomBytesFree(char* data, void* hint) { } -template +template void RandomBytesWork(uv_work_t* work_req) { - RandomBytesRequest* req = - container_of(work_req, RandomBytesRequest, work_req_); + RandomBytesRequest* req = container_of(work_req, + RandomBytesRequest, + work_req_); + int r; - int r = generator(reinterpret_cast(req->data_), req->size_); + if (pseudoRandom == true) { + r = RAND_pseudo_bytes(reinterpret_cast(req->data_), + req->size_); + } else { + r = RAND_bytes(reinterpret_cast(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; - - 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(-1); } } @@ -4414,10 +4412,10 @@ void RandomBytesCheck(RandomBytesRequest* req, Local argv[2]) { } -template 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 argv[2]; @@ -4428,7 +4426,7 @@ void RandomBytesAfter(uv_work_t* work_req) { } -template +template Handle RandomBytes(const Arguments& args) { HandleScope scope; @@ -4452,14 +4450,14 @@ Handle RandomBytes(const Arguments& args) { uv_queue_work(uv_default_loop(), &req->work_req_, - RandomBytesWork, - RandomBytesAfter); + RandomBytesWork, + RandomBytesAfter); return req->obj_; } else { Local argv[2]; - RandomBytesWork(&req->work_req_); + RandomBytesWork(&req->work_req_); RandomBytesCheck(req, argv); delete req; @@ -4508,8 +4506,8 @@ void InitCrypto(Handle target) { Verify::Initialize(target); NODE_SET_METHOD(target, "PBKDF2", PBKDF2); - NODE_SET_METHOD(target, "randomBytes", RandomBytes); - NODE_SET_METHOD(target, "pseudoRandomBytes", RandomBytes); + NODE_SET_METHOD(target, "randomBytes", RandomBytes); + NODE_SET_METHOD(target, "pseudoRandomBytes", RandomBytes); subject_symbol = NODE_PSYMBOL("subject"); issuer_symbol = NODE_PSYMBOL("issuer");