Skip to content

Commit 8e823bc

Browse files
GnaphronGtrevnorris
authored andcommitted
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 <[email protected]>
1 parent 4c36f3e commit 8e823bc

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/buffer.js

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

477477
return (this[offset] * 0x1000000) +
478478
((this[offset + 1] << 16) |
479-
(this[offset + 2] << 8)) |
480-
(this[offset + 3]);
479+
(this[offset + 2] << 8) |
480+
(this[offset + 3]) >>> 0);
481481
};
482482

483483

test/simple/test-buffer.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,13 @@ var buf = new Buffer(0);
914914
assert.throws(function() { buf.readUInt8(0); }, RangeError);
915915
assert.throws(function() { buf.readInt8(0); }, RangeError);
916916

917+
var buf = new Buffer([0xFF]);
918+
919+
assert.equal(buf.readUInt8(0), 255);
920+
assert.equal(buf.readInt8(0), -1);
921+
922+
923+
917924
[16, 32].forEach(function(bits) {
918925
var buf = new Buffer(bits / 8 - 1);
919926

@@ -934,6 +941,22 @@ assert.throws(function() { buf.readInt8(0); }, RangeError);
934941
'readInt' + bits + 'LE()');
935942
});
936943

944+
[16, 32].forEach(function(bits) {
945+
var buf = new Buffer([0xFF, 0xFF, 0xFF, 0xFF]);
946+
947+
assert.equal(buf['readUInt' + bits + 'BE'](0),
948+
(0xFFFFFFFF >>> (32 - bits)));
949+
950+
assert.equal(buf['readUInt' + bits + 'LE'](0),
951+
(0xFFFFFFFF >>> (32 - bits)));
952+
953+
assert.equal(buf['readInt' + bits + 'BE'](0),
954+
(0xFFFFFFFF >> (32 - bits)));
955+
956+
assert.equal(buf['readInt' + bits + 'LE'](0),
957+
(0xFFFFFFFF >> (32 - bits)));
958+
});
959+
937960
// test Buffer slice
938961
(function() {
939962
var buf = new Buffer('0123456789');

0 commit comments

Comments
 (0)