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 1 commit
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.trunc(this.highWaterMark);
Copy link
Contributor

@mscdex mscdex May 14, 2017

Choose a reason for hiding this comment

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

Actually, I think we may want to use Math.floor() instead, it seems to be significantly faster (~6x at least on V8 5.8).

Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

Choose a reason for hiding this comment

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

@tniessen can you update that?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.


// 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.trunc(this.highWaterMark);

// drain event flag.
this.needDrain = false;
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-streams-highwatermark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

require('../common');
const assert = require('assert');
const stream = require('stream');

const readable = stream.Readable({ highWaterMark: 2147483648 });
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you move all of the 2147483648s into a variable, and add a comment on why that specific value was chosen. That should prevent anyone from changing it to a small value in the future.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think a better value to test is Number.MAX_SAFE_INTEGER.

Copy link
Member Author

Choose a reason for hiding this comment

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

@mscdex Done.

assert.strictEqual(readable._readableState.highWaterMark, 2147483648);

const writable = stream.Writable({ highWaterMark: 2147483648 });
assert.strictEqual(writable._writableState.highWaterMark, 2147483648);