buffer: coerce offset to integer

The offset was formerly coerced to a integer and this reimplements
that.

PR-URL: https://github.com/nodejs/node/pull/18215
Fixes: https://github.com/nodejs/node/issues/18208
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
Ruben Bridgewater 2018-01-17 23:08:52 +01:00
parent 373e893ee0
commit 7a23fc0760
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
2 changed files with 87 additions and 84 deletions

View File

@ -1272,6 +1272,7 @@ function toFloat(x) {
Buffer.prototype.readDoubleBE = function(offset, noAssert) { Buffer.prototype.readDoubleBE = function(offset, noAssert) {
offset = offset >>> 0;
const x1 = this.readUInt32BE(offset + 0, noAssert); const x1 = this.readUInt32BE(offset + 0, noAssert);
const x0 = this.readUInt32BE(offset + 4, noAssert); const x0 = this.readUInt32BE(offset + 4, noAssert);
return toDouble(x0, x1); return toDouble(x0, x1);
@ -1279,6 +1280,7 @@ Buffer.prototype.readDoubleBE = function(offset, noAssert) {
Buffer.prototype.readDoubleLE = function(offset, noAssert) { Buffer.prototype.readDoubleLE = function(offset, noAssert) {
offset = offset >>> 0;
const x0 = this.readUInt32LE(offset + 0, noAssert); const x0 = this.readUInt32LE(offset + 0, noAssert);
const x1 = this.readUInt32LE(offset + 4, noAssert); const x1 = this.readUInt32LE(offset + 4, noAssert);
return toDouble(x0, x1); return toDouble(x0, x1);
@ -1286,11 +1288,13 @@ Buffer.prototype.readDoubleLE = function(offset, noAssert) {
Buffer.prototype.readFloatBE = function(offset, noAssert) { Buffer.prototype.readFloatBE = function(offset, noAssert) {
offset = offset >>> 0;
return toFloat(this.readUInt32BE(offset, noAssert)); return toFloat(this.readUInt32BE(offset, noAssert));
}; };
Buffer.prototype.readFloatLE = function(offset, noAssert) { Buffer.prototype.readFloatLE = function(offset, noAssert) {
offset = offset >>> 0;
return toFloat(this.readUInt32LE(offset, noAssert)); return toFloat(this.readUInt32LE(offset, noAssert));
}; };

View File

@ -29,8 +29,7 @@ const assert = require('assert');
/* /*
* Test (64 bit) double * Test (64 bit) double
*/ */
function test(clazz) { const buffer = Buffer.allocUnsafe(8);
const buffer = new clazz(8);
buffer[0] = 0x55; buffer[0] = 0x55;
buffer[1] = 0x55; buffer[1] = 0x55;
@ -120,11 +119,11 @@ function test(clazz) {
assert.strictEqual(3.0418e-319, buffer.readDoubleBE(0)); assert.strictEqual(3.0418e-319, buffer.readDoubleBE(0));
assert.strictEqual(Infinity, buffer.readDoubleLE(0)); assert.strictEqual(Infinity, buffer.readDoubleLE(0));
buffer[6] = 0xf0;
buffer[7] = 0xff; buffer[7] = 0xff;
assert.strictEqual(3.04814e-319, buffer.readDoubleBE(0)); assert.strictEqual(3.04814e-319, buffer.readDoubleBE(0));
assert.strictEqual(-Infinity, buffer.readDoubleLE(0)); assert.strictEqual(-Infinity, buffer.readDoubleLE(0));
}
buffer.writeDoubleBE(246800);
test(Buffer); assert.strictEqual(buffer.readDoubleBE(), 246800);
assert.strictEqual(buffer.readDoubleBE(0.7), 246800);
assert.strictEqual(buffer.readDoubleBE(NaN), 246800);