From 3c9fb3ec1af991b1b1eb7105b2a790549ca2b844 Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 15 Mar 2011 11:34:47 -0700 Subject: [PATCH] Fix invalid end handling for SlowBuffer#hexSlice --- lib/buffer.js | 2 +- test/simple/test-buffer.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/buffer.js b/lib/buffer.js index f4f916ff45..5f0298185a 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -42,7 +42,7 @@ SlowBuffer.prototype.hexSlice = function(start, end) { var len = this.length; if (!start || start < 0) start = 0; - if (end < 0) end = len - start; + if (!end || end < 0 || end > len) end = len - 1; var out = ''; for (var i = start; i < end; i ++) { diff --git a/test/simple/test-buffer.js b/test/simple/test-buffer.js index 72fd2a43cf..9300d51855 100644 --- a/test/simple/test-buffer.js +++ b/test/simple/test-buffer.js @@ -462,3 +462,12 @@ var hexb2 = new Buffer(hexStr, 'hex'); for (var i = 0; i < 256; 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);