Skip to content

Commit 38f6fcd

Browse files
committed
buffer: fix sign overflow in readUIn32BE
`|` operation takes precendence on `+`, which will result in `new Buffer('ffffffff', 16).readUInt32BE(0)` returning `-1` instead of `ffffffff`.
1 parent 338ba2b commit 38f6fcd

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

lib/buffer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,8 @@ Buffer.prototype.readUInt32BE = function(offset, noAssert) {
621621

622622
return (this[offset] * 0x1000000) +
623623
((this[offset + 1] << 16) |
624-
(this[offset + 2] << 8)) |
625-
(this[offset + 3]);
624+
(this[offset + 2] << 8) |
625+
this[offset + 3]);
626626
};
627627

628628

test/simple/test-buffer.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,22 @@ assert.throws(function() { buf.readInt8(0); }, /beyond buffer length/);
964964
);
965965
});
966966

967+
[16, 32].forEach(function(bits) {
968+
var buf = new Buffer([0xFF, 0xFF, 0xFF, 0xFF]);
969+
970+
assert.equal(buf['readUInt' + bits + 'BE'](0),
971+
(0xFFFFFFFF >>> (32 - bits)));
972+
973+
assert.equal(buf['readUInt' + bits + 'LE'](0),
974+
(0xFFFFFFFF >>> (32 - bits)));
975+
976+
assert.equal(buf['readInt' + bits + 'BE'](0),
977+
(0xFFFFFFFF >> (32 - bits)));
978+
979+
assert.equal(buf['readInt' + bits + 'LE'](0),
980+
(0xFFFFFFFF >> (32 - bits)));
981+
});
982+
967983
// SlowBuffer sanity checks.
968984
assert.throws(function() {
969985
var len = 0xfffff;

0 commit comments

Comments
 (0)