Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
buffer: fix offset checks
Browse files Browse the repository at this point in the history
Fixed offset checks in Buffer.readInt32LE() and Buffer.readInt32BE()
functions.
  • Loading branch information
morkai authored and isaacs committed Apr 8, 2013
1 parent c93af86 commit 2e28832
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,14 +737,14 @@ function readInt32(buffer, offset, isBigEndian) {

Buffer.prototype.readInt32LE = function(offset, noAssert) {
if (!noAssert)
checkOffset(offset, 2, this.length);
checkOffset(offset, 4, this.length);
return readInt32(this, offset, false);
};


Buffer.prototype.readInt32BE = function(offset, noAssert) {
if (!noAssert)
checkOffset(offset, 2, this.length);
checkOffset(offset, 4, this.length);
return readInt32(this, offset, true);
};

Expand Down
33 changes: 33 additions & 0 deletions test/simple/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,39 @@ assert.throws(function() {
buf.writeFloatLE(0.0, -1);
}, /offset is not uint/);

// offset checks
var buf = new Buffer(0);

assert.throws(function() { buf.readUInt8(0); }, /beyond buffer length/);
assert.throws(function() { buf.readInt8(0); }, /beyond buffer length/);

[16, 32].forEach(function(bits) {
var buf = new Buffer(bits / 8 - 1);

assert.throws(
function() { buf['readUInt' + bits + 'BE'](0); },
/beyond buffer length/,
'readUInt' + bits + 'BE'
);

assert.throws(
function() { buf['readUInt' + bits + 'LE'](0); },
/beyond buffer length/,
'readUInt' + bits + 'LE'
);

assert.throws(
function() { buf['readInt' + bits + 'BE'](0); },
/beyond buffer length/,
'readInt' + bits + 'BE()'
);

assert.throws(
function() { buf['readInt' + bits + 'LE'](0); },
/beyond buffer length/,
'readInt' + bits + 'LE()'
);
});

// SlowBuffer sanity checks.
assert.throws(function() {
Expand Down

0 comments on commit 2e28832

Please sign in to comment.