Browse Source

Fix invalid end handling for SlowBuffer#hexSlice

v0.7.4-release
isaacs 14 years ago
committed by Ryan Dahl
parent
commit
3c9fb3ec1a
  1. 2
      lib/buffer.js
  2. 9
      test/simple/test-buffer.js

2
lib/buffer.js

@ -42,7 +42,7 @@ SlowBuffer.prototype.hexSlice = function(start, end) {
var len = this.length; var len = this.length;
if (!start || start < 0) start = 0; if (!start || start < 0) start = 0;
if (end < 0) end = len - start; if (!end || end < 0 || end > len) end = len - 1;
var out = ''; var out = '';
for (var i = start; i < end; i ++) { for (var i = start; i < end; i ++) {

9
test/simple/test-buffer.js

@ -462,3 +462,12 @@ var hexb2 = new Buffer(hexStr, 'hex');
for (var i = 0; i < 256; i ++) { for (var i = 0; i < 256; i ++) {
assert.equal(hexb2[i], hexb[i]); assert.equal(hexb2[i], hexb[i]);
} }
// test an invalid slice end.
console.log('Try to slice off the end of the buffer');
var b = new Buffer([1,2,3,4,5]);
var b2 = b.toString('hex', 1, 10000);
var b3 = b.toString('hex', 1, 5);
var b4 = b.toString('hex', 1);
assert.equal(b2, b3);
assert.equal(b2, b4);

Loading…
Cancel
Save