Skip to content

Commit

Permalink
stream: check highWaterMark type and range
Browse files Browse the repository at this point in the history
Adds checks for the type of "highWaterMark" and restricts it to
non-negative values.

Refs: nodejs#12593
  • Loading branch information
tniessen committed May 16, 2017
1 parent 11918c4 commit b09ff75
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
18 changes: 14 additions & 4 deletions lib/_stream_readable.js
Expand Up @@ -68,11 +68,21 @@ function ReadableState(options, stream) {
// the point at which it stops calling _read() to fill the buffer
// Note: 0 is a valid value, means "don't call _read preemptively ever"
var hwm = options.highWaterMark;
var defaultHwm = this.objectMode ? 16 : 16 * 1024;
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
if (hwm == null) {
var defaultHwm = this.objectMode ? 16 : 16 * 1024;
this.highWaterMark = defaultHwm;
} else {
if (typeof hwm !== 'number') {
throw new TypeError('"highWaterMark" must be a number');
}

// cast to ints.
this.highWaterMark = Math.floor(this.highWaterMark);
if (hwm < 0) {
throw new RangeError('"highWaterMark" must not be negative');
}

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

// 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
18 changes: 14 additions & 4 deletions lib/_stream_writable.js
Expand Up @@ -51,11 +51,21 @@ function WritableState(options, stream) {
// Note: 0 is a valid value, means that we always return false if
// the entire buffer is not flushed immediately on write()
var hwm = options.highWaterMark;
var defaultHwm = this.objectMode ? 16 : 16 * 1024;
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
if (hwm == null) {
var defaultHwm = this.objectMode ? 16 : 16 * 1024;
this.highWaterMark = defaultHwm;
} else {
if (typeof hwm !== 'number') {
throw new TypeError('"highWaterMark" must be a number');
}

// cast to ints.
this.highWaterMark = Math.floor(this.highWaterMark);
if (hwm < 0) {
throw new RangeError('"highWaterMark" must not be negative');
}

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

// drain event flag.
this.needDrain = false;
Expand Down

0 comments on commit b09ff75

Please sign in to comment.