buffer: return uint if MSB is 1 in readUInt32
Fix issue where a signed integer is returned. Example: var b = new Buffer(4); b.writeUInt32BE(0xffffffff); b.readUInt32BE(0) == -1 Signed-off-by: Trevor Norris <trev.norris@gmail.com>
This commit is contained in:
parent
4c36f3e7e6
commit
8e823bcbe6
@ -476,8 +476,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]) >>> 0);
|
||||
};
|
||||
|
||||
|
||||
|
@ -914,6 +914,13 @@ var buf = new Buffer(0);
|
||||
assert.throws(function() { buf.readUInt8(0); }, RangeError);
|
||||
assert.throws(function() { buf.readInt8(0); }, RangeError);
|
||||
|
||||
var buf = new Buffer([0xFF]);
|
||||
|
||||
assert.equal(buf.readUInt8(0), 255);
|
||||
assert.equal(buf.readInt8(0), -1);
|
||||
|
||||
|
||||
|
||||
[16, 32].forEach(function(bits) {
|
||||
var buf = new Buffer(bits / 8 - 1);
|
||||
|
||||
@ -934,6 +941,22 @@ assert.throws(function() { buf.readInt8(0); }, RangeError);
|
||||
'readInt' + bits + 'LE()');
|
||||
});
|
||||
|
||||
[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)));
|
||||
});
|
||||
|
||||
// test Buffer slice
|
||||
(function() {
|
||||
var buf = new Buffer('0123456789');
|
||||
|
Loading…
x
Reference in New Issue
Block a user