Skip to content

Commit 11918c4

Browse files
tniessenmcollina
authored andcommitted
stream: fix highWaterMark integer overflow
Fixes integer overflows when supplying values exceeding MAX_SAFE_INTEGER for highWaterMark. PR-URL: #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>
1 parent 7906ed5 commit 11918c4

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

lib/_stream_readable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function ReadableState(options, stream) {
7272
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
7373

7474
// cast to ints.
75-
this.highWaterMark = ~~this.highWaterMark;
75+
this.highWaterMark = Math.floor(this.highWaterMark);
7676

7777
// A linked list is used to store data chunks instead of an array because the
7878
// linked list can remove elements from the beginning faster than

lib/_stream_writable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function WritableState(options, stream) {
5555
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
5656

5757
// cast to ints.
58-
this.highWaterMark = ~~this.highWaterMark;
58+
this.highWaterMark = Math.floor(this.highWaterMark);
5959

6060
// drain event flag.
6161
this.needDrain = false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
require('../common');
3+
4+
// This test ensures that the stream implementation correctly handles values
5+
// for highWaterMark which exceed the range of signed 32 bit integers.
6+
7+
const assert = require('assert');
8+
const stream = require('stream');
9+
10+
// This number exceeds the range of 32 bit integer arithmetic but should still
11+
// be handled correctly.
12+
const ovfl = Number.MAX_SAFE_INTEGER;
13+
14+
const readable = stream.Readable({ highWaterMark: ovfl });
15+
assert.strictEqual(readable._readableState.highWaterMark, ovfl);
16+
17+
const writable = stream.Writable({ highWaterMark: ovfl });
18+
assert.strictEqual(writable._writableState.highWaterMark, ovfl);

0 commit comments

Comments
 (0)