Skip to content

Commit ce04b73

Browse files
skomskirvagg
authored andcommitted
src: only memcmp if length > 0 in Buffer::Compare
Both pointer arguments to memcmp are defined as non-null and compiler optimizes upon that. PR-URL: #2544 Reviewed-By: trevnorris - Trevor Norris <trev.norris@gmail.com> Reviewed-By: thefourtheye - Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
1 parent 42e075a commit ce04b73

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/node_buffer.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ void Compare(const FunctionCallbackInfo<Value> &args) {
835835

836836
size_t cmp_length = MIN(obj_a_length, obj_b_length);
837837

838-
int32_t val = memcmp(obj_a_data, obj_b_data, cmp_length);
838+
int val = cmp_length > 0 ? memcmp(obj_a_data, obj_b_data, cmp_length) : 0;
839839

840840
// Normalize val to be an integer in the range of [1, -1] since
841841
// implementations of memcmp() can vary by platform.

test/parallel/test-buffer.js

+3
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,9 @@ assert.equal(Buffer.compare(d, b), 1);
11371137
assert.equal(Buffer.compare(b, d), -1);
11381138
assert.equal(Buffer.compare(c, c), 0);
11391139

1140+
assert.equal(Buffer.compare(Buffer(0), Buffer(0)), 0);
1141+
assert.equal(Buffer.compare(Buffer(0), Buffer(1)), -1);
1142+
assert.equal(Buffer.compare(Buffer(1), Buffer(0)), 1);
11401143

11411144
assert.throws(function() {
11421145
var b = new Buffer(1);

0 commit comments

Comments
 (0)