Skip to content

Commit

Permalink
buffer: don't compare same buffers
Browse files Browse the repository at this point in the history
PR-URL: #742

Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Trevor Norris <[email protected]>
  • Loading branch information
vkurchatkin authored and micnic committed Feb 6, 2015
1 parent 847b9d2 commit 1cd1d7a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ Buffer.compare = function compare(a, b) {
!(b instanceof Buffer))
throw new TypeError('Arguments must be Buffers');

if (a === b)
return 0;

return internal.compare(a, b);
};

Expand Down Expand Up @@ -268,6 +271,9 @@ Buffer.prototype.equals = function equals(b) {
if (!(b instanceof Buffer))
throw new TypeError('Argument must be a Buffer');

if (this === b)
return true;

return internal.compare(this, b) === 0;
};

Expand All @@ -289,6 +295,9 @@ Buffer.prototype.compare = function compare(b) {
if (!(b instanceof Buffer))
throw new TypeError('Argument must be a Buffer');

if (this === b)
return 0;

return internal.compare(this, b);
};

Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1128,11 +1128,14 @@ assert.equal(b.compare(c), -1);
assert.equal(c.compare(d), 1);
assert.equal(d.compare(b), 1);
assert.equal(b.compare(d), -1);
assert.equal(b.compare(b), 0);

assert.equal(Buffer.compare(b, c), -1);
assert.equal(Buffer.compare(c, d), 1);
assert.equal(Buffer.compare(d, b), 1);
assert.equal(Buffer.compare(b, d), -1);
assert.equal(Buffer.compare(c, c), 0);


assert.throws(function() {
var b = new Buffer(1);
Expand All @@ -1158,6 +1161,7 @@ var e = new Buffer(6).fill('abcdef');
assert.ok(b.equals(c));
assert.ok(!c.equals(d));
assert.ok(!d.equals(e));
assert.ok(d.equals(d));

assert.throws(function() {
var b = new Buffer(1);
Expand Down

0 comments on commit 1cd1d7a

Please sign in to comment.