buffer: don't compare same buffers
authorVladimir Kurchatkin <vladimir.kurchatkin@gmail.com>
Fri, 6 Feb 2015 15:54:29 +0000 (18:54 +0300)
committermicnic <micnic90@gmail.com>
Fri, 6 Feb 2015 22:08:40 +0000 (00:08 +0200)
PR-URL: https://github.com/iojs/io.js/pull/742

Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
lib/buffer.js
test/parallel/test-buffer.js

index 19208b3..02b3076 100644 (file)
@@ -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);
 };
 
@@ -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;
 };
 
@@ -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);
 };
 
index 3c25b8e..1c2c425 100644 (file)
@@ -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);
@@ -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);