stream: fix highWaterMark integer overflow
Fixes integer overflows when supplying values exceeding MAX_SAFE_INTEGER for highWaterMark. PR-URL: https://github.com/nodejs/node/pull/12593 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luca Maraschi <luca.maraschi@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
7906ed50fa
commit
11918c4aed
@ -72,7 +72,7 @@ function ReadableState(options, stream) {
|
|||||||
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
|
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
|
||||||
|
|
||||||
// cast to ints.
|
// cast to ints.
|
||||||
this.highWaterMark = ~~this.highWaterMark;
|
this.highWaterMark = Math.floor(this.highWaterMark);
|
||||||
|
|
||||||
// A linked list is used to store data chunks instead of an array because the
|
// A linked list is used to store data chunks instead of an array because the
|
||||||
// linked list can remove elements from the beginning faster than
|
// linked list can remove elements from the beginning faster than
|
||||||
|
@ -55,7 +55,7 @@ function WritableState(options, stream) {
|
|||||||
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
|
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
|
||||||
|
|
||||||
// cast to ints.
|
// cast to ints.
|
||||||
this.highWaterMark = ~~this.highWaterMark;
|
this.highWaterMark = Math.floor(this.highWaterMark);
|
||||||
|
|
||||||
// drain event flag.
|
// drain event flag.
|
||||||
this.needDrain = false;
|
this.needDrain = false;
|
||||||
|
18
test/parallel/test-streams-highwatermark.js
Normal file
18
test/parallel/test-streams-highwatermark.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
'use strict';
|
||||||
|
require('../common');
|
||||||
|
|
||||||
|
// This test ensures that the stream implementation correctly handles values
|
||||||
|
// for highWaterMark which exceed the range of signed 32 bit integers.
|
||||||
|
|
||||||
|
const assert = require('assert');
|
||||||
|
const stream = require('stream');
|
||||||
|
|
||||||
|
// This number exceeds the range of 32 bit integer arithmetic but should still
|
||||||
|
// be handled correctly.
|
||||||
|
const ovfl = Number.MAX_SAFE_INTEGER;
|
||||||
|
|
||||||
|
const readable = stream.Readable({ highWaterMark: ovfl });
|
||||||
|
assert.strictEqual(readable._readableState.highWaterMark, ovfl);
|
||||||
|
|
||||||
|
const writable = stream.Writable({ highWaterMark: ovfl });
|
||||||
|
assert.strictEqual(writable._writableState.highWaterMark, ovfl);
|
Loading…
x
Reference in New Issue
Block a user