Skip to content

Commit 5de6a6e

Browse files
committed
src: only memcmp if length > 0 in Buffer::Compare
Both pointer arguments to memcmp are defined as non-null and compiler optimizes upon that
1 parent 96a2b2d commit 5de6a6e

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/node_buffer.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,8 @@ 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)
839+
: 0;
839840

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

test/parallel/test-buffer.js

Lines changed: 3 additions & 0 deletions
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)