From e3740e452402892c1ce03bbbb4807e34f31558ad Mon Sep 17 00:00:00 2001 From: Karl Skomski Date: Fri, 21 Aug 2015 13:31:24 +0200 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/2544 Reviewed-By: trevnorris - Trevor Norris Reviewed-By: thefourtheye - Sakthipriyan Vairamani --- src/node_buffer.cc | 2 +- test/parallel/test-buffer.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index c8be5b5a44..54c4711a1e 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -835,7 +835,7 @@ void Compare(const FunctionCallbackInfo &args) { size_t cmp_length = MIN(obj_a_length, obj_b_length); - int32_t val = memcmp(obj_a_data, obj_b_data, cmp_length); + int val = cmp_length > 0 ? memcmp(obj_a_data, obj_b_data, cmp_length) : 0; // Normalize val to be an integer in the range of [1, -1] since // implementations of memcmp() can vary by platform. diff --git a/test/parallel/test-buffer.js b/test/parallel/test-buffer.js index a6730deab2..c8f9dce775 100644 --- a/test/parallel/test-buffer.js +++ b/test/parallel/test-buffer.js @@ -1137,6 +1137,9 @@ assert.equal(Buffer.compare(d, b), 1); assert.equal(Buffer.compare(b, d), -1); assert.equal(Buffer.compare(c, c), 0); +assert.equal(Buffer.compare(Buffer(0), Buffer(0)), 0); +assert.equal(Buffer.compare(Buffer(0), Buffer(1)), -1); +assert.equal(Buffer.compare(Buffer(1), Buffer(0)), 1); assert.throws(function() { var b = new Buffer(1);