Browse Source

buffer: fix value check for writeUInt{B,L}E

Fixes: https://github.com/nodejs/node/issues/3497
PR-URL: https://github.com/nodejs/node/pull/3500
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
process-exit-stdio-flushing
Trevor Norris 9 years ago
parent
commit
3308e5ea2a
  1. 12
      lib/buffer.js
  2. 19
      test/parallel/test-writeuint.js

12
lib/buffer.js

@ -832,8 +832,10 @@ Buffer.prototype.writeUIntLE = function(value, offset, byteLength, noAssert) {
value = +value; value = +value;
offset = offset >>> 0; offset = offset >>> 0;
byteLength = byteLength >>> 0; byteLength = byteLength >>> 0;
if (!noAssert) if (!noAssert) {
checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0); const maxBytes = Math.pow(2, 8 * byteLength) - 1;
checkInt(this, value, offset, byteLength, maxBytes, 0);
}
var mul = 1; var mul = 1;
var i = 0; var i = 0;
@ -849,8 +851,10 @@ Buffer.prototype.writeUIntBE = function(value, offset, byteLength, noAssert) {
value = +value; value = +value;
offset = offset >>> 0; offset = offset >>> 0;
byteLength = byteLength >>> 0; byteLength = byteLength >>> 0;
if (!noAssert) if (!noAssert) {
checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0); const maxBytes = Math.pow(2, 8 * byteLength) - 1;
checkInt(this, value, offset, byteLength, maxBytes, 0);
}
var i = byteLength - 1; var i = byteLength - 1;
var mul = 1; var mul = 1;

19
test/parallel/test-writeuint.js

@ -122,6 +122,25 @@ function test32(clazz) {
} }
function testUint(clazz) {
const data = new clazz(8);
var val = 1;
// Test 0 to 5 bytes.
for (var i = 0; i <= 5; i++) {
const errmsg = `byteLength: ${i}`;
ASSERT.throws(function() {
data.writeUIntBE(val, 0, i);
}, /value is out of bounds/, errmsg);
ASSERT.throws(function() {
data.writeUIntLE(val, 0, i);
}, /value is out of bounds/, errmsg);
val *= 0x100;
}
}
test8(Buffer); test8(Buffer);
test16(Buffer); test16(Buffer);
test32(Buffer); test32(Buffer);
testUint(Buffer);

Loading…
Cancel
Save