Skip to content

Commit eec8c2e

Browse files
committed
crypto: fix -Wtautological-compare warning
1 parent be5a8e2 commit eec8c2e

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

src/node_crypto.cc

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4346,8 +4346,6 @@ PBKDF2(const Arguments& args) {
43464346
}
43474347

43484348

4349-
typedef int (*RandomBytesGenerator)(unsigned char* buf, int size);
4350-
43514349
struct RandomBytesRequest {
43524350
~RandomBytesRequest();
43534351
Persistent<Object> obj_;
@@ -4370,26 +4368,26 @@ void RandomBytesFree(char* data, void* hint) {
43704368
}
43714369

43724370

4373-
template <RandomBytesGenerator generator>
4371+
template <bool pseudoRandom>
43744372
void RandomBytesWork(uv_work_t* work_req) {
4375-
RandomBytesRequest* req =
4376-
container_of(work_req, RandomBytesRequest, work_req_);
4377-
4378-
int r = generator(reinterpret_cast<unsigned char*>(req->data_), req->size_);
4379-
4380-
switch (r) {
4381-
case 0:
4382-
// RAND_bytes() returns 0 on error, RAND_pseudo_bytes() returns 0
4383-
// when the result is not cryptographically strong - the latter
4384-
// sucks but is not an error
4385-
if (generator == RAND_bytes)
4386-
req->error_ = ERR_get_error();
4387-
break;
4373+
RandomBytesRequest* req = container_of(work_req,
4374+
RandomBytesRequest,
4375+
work_req_);
4376+
int r;
4377+
4378+
if (pseudoRandom == true) {
4379+
r = RAND_pseudo_bytes(reinterpret_cast<unsigned char*>(req->data_),
4380+
req->size_);
4381+
} else {
4382+
r = RAND_bytes(reinterpret_cast<unsigned char*>(req->data_), req->size_);
4383+
}
43884384

4389-
case -1:
4390-
// not supported - can this actually happen?
4391-
req->error_ = (unsigned long) -1;
4392-
break;
4385+
// RAND_bytes() returns 0 on error. RAND_pseudo_bytes() returns 0 when the
4386+
// result is not cryptographically strong - but that's not an error.
4387+
if (r == 0 && pseudoRandom == false) {
4388+
req->error_ = ERR_get_error();
4389+
} else if (r == -1) {
4390+
req->error_ = static_cast<unsigned long>(-1);
43934391
}
43944392
}
43954393

@@ -4414,10 +4412,10 @@ void RandomBytesCheck(RandomBytesRequest* req, Local<Value> argv[2]) {
44144412
}
44154413

44164414

4417-
template <RandomBytesGenerator generator>
44184415
void RandomBytesAfter(uv_work_t* work_req) {
4419-
RandomBytesRequest* req =
4420-
container_of(work_req, RandomBytesRequest, work_req_);
4416+
RandomBytesRequest* req = container_of(work_req,
4417+
RandomBytesRequest,
4418+
work_req_);
44214419

44224420
HandleScope scope;
44234421
Local<Value> argv[2];
@@ -4428,7 +4426,7 @@ void RandomBytesAfter(uv_work_t* work_req) {
44284426
}
44294427

44304428

4431-
template <RandomBytesGenerator generator>
4429+
template <bool pseudoRandom>
44324430
Handle<Value> RandomBytes(const Arguments& args) {
44334431
HandleScope scope;
44344432

@@ -4452,14 +4450,14 @@ Handle<Value> RandomBytes(const Arguments& args) {
44524450

44534451
uv_queue_work(uv_default_loop(),
44544452
&req->work_req_,
4455-
RandomBytesWork<generator>,
4456-
RandomBytesAfter<generator>);
4453+
RandomBytesWork<pseudoRandom>,
4454+
RandomBytesAfter);
44574455

44584456
return req->obj_;
44594457
}
44604458
else {
44614459
Local<Value> argv[2];
4462-
RandomBytesWork<generator>(&req->work_req_);
4460+
RandomBytesWork<pseudoRandom>(&req->work_req_);
44634461
RandomBytesCheck(req, argv);
44644462
delete req;
44654463

@@ -4508,8 +4506,8 @@ void InitCrypto(Handle<Object> target) {
45084506
Verify::Initialize(target);
45094507

45104508
NODE_SET_METHOD(target, "PBKDF2", PBKDF2);
4511-
NODE_SET_METHOD(target, "randomBytes", RandomBytes<RAND_bytes>);
4512-
NODE_SET_METHOD(target, "pseudoRandomBytes", RandomBytes<RAND_pseudo_bytes>);
4509+
NODE_SET_METHOD(target, "randomBytes", RandomBytes<false>);
4510+
NODE_SET_METHOD(target, "pseudoRandomBytes", RandomBytes<true>);
45134511

45144512
subject_symbol = NODE_PSYMBOL("subject");
45154513
issuer_symbol = NODE_PSYMBOL("issuer");

0 commit comments

Comments
 (0)