Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: fix highWaterMark integer overflow #12593

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function ReadableState(options, stream) {
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;

// cast to ints.
this.highWaterMark = ~~this.highWaterMark;
this.highWaterMark = Math.floor(this.highWaterMark);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: This changes the result when passed a negative floating point number.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thefourtheye what do you suggest? Can you make an example?
This was just changed at @mscdex suggestion from Math.trunc.

All negative values are interpreted as 0 by the machinery, so nothing should change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcollina Oh thats okay. This is just a nit. I am just making an observation here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll be even less of an issue once we start throwing on invalid values.

Copy link
Member Author

@tniessen tniessen May 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct, but not an actual problem. According to @mcollina:

Any negative value is interpreted by the stream machinery as a 0

Therefore, it does not matter whether the value is Math.floor(-233.5) = -234 or ~~(-233.5) = -233, not even in the special case of Math.floor(-0.5) = -1 whilst ~~(-0.5) = 0.

We are planning to throw on negative values anyway.

Edit: Sorry, the last three messages were invisible to me until now, I did not know this had already been discussed with @mcollina and @mscdex.


// 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
Expand Down
2 changes: 1 addition & 1 deletion lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function WritableState(options, stream) {
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;

// cast to ints.
this.highWaterMark = ~~this.highWaterMark;
this.highWaterMark = Math.floor(this.highWaterMark);

// drain event flag.
this.needDrain = false;
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-streams-highwatermark.js
Original file line number Diff line number Diff line change
@@ -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);