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

Commit

Permalink
buffer: fix sign overflow in readUIn32BE
Browse files Browse the repository at this point in the history
`|` operation takes precendence on `+`, which will result in
`new Buffer('ffffffff', 16).readUInt32BE(0)` returning `-1` instead of
`ffffffff`.
  • Loading branch information
indutny committed Jul 29, 2014
1 parent 338ba2b commit 38f6fcd
Show file tree
Hide file tree
Showing 2 changed files with 18 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 @@ -621,8 +621,8 @@ Buffer.prototype.readUInt32BE = function(offset, noAssert) {

return (this[offset] * 0x1000000) +
((this[offset + 1] << 16) |
(this[offset + 2] << 8)) |
(this[offset + 3]);
(this[offset + 2] << 8) |
this[offset + 3]);
};


Expand Down
16 changes: 16 additions & 0 deletions test/simple/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,22 @@ assert.throws(function() { buf.readInt8(0); }, /beyond buffer length/);
);
});

[16, 32].forEach(function(bits) {
var buf = new Buffer([0xFF, 0xFF, 0xFF, 0xFF]);

assert.equal(buf['readUInt' + bits + 'BE'](0),
(0xFFFFFFFF >>> (32 - bits)));

assert.equal(buf['readUInt' + bits + 'LE'](0),
(0xFFFFFFFF >>> (32 - bits)));

assert.equal(buf['readInt' + bits + 'BE'](0),
(0xFFFFFFFF >> (32 - bits)));

assert.equal(buf['readInt' + bits + 'LE'](0),
(0xFFFFFFFF >> (32 - bits)));
});

// SlowBuffer sanity checks.
assert.throws(function() {
var len = 0xfffff;
Expand Down

0 comments on commit 38f6fcd

Please sign in to comment.