Skip to content

Commit

Permalink
fs: extract out validateOffsetLengthRead function
Browse files Browse the repository at this point in the history
PR-URL: #17682
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
maclover7 authored and joyeecheung committed Jan 11, 2018
1 parent b9b8294 commit fc8c1b1
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ function validateFd(fd) {
}
}

function validateOffsetLengthRead(offset, length, bufferLength) {
let err;

if (offset < 0 || offset >= bufferLength) {
err = new errors.RangeError('ERR_OUT_OF_RANGE', 'offset');
} else if (length < 0 || offset + length > bufferLength) {
err = new errors.RangeError('ERR_OUT_OF_RANGE', 'length');
}

if (err !== undefined) {
Error.captureStackTrace(err, validateOffsetLengthRead);
throw err;
}
}

// Special case of `makeCallback()` that is specific to async `*stat()` calls as
// an optimization, since the data passed back to the callback needs to be
// transformed anyway.
Expand Down Expand Up @@ -743,11 +758,7 @@ fs.read = function(fd, buffer, offset, length, position, callback) {
});
}

if (offset < 0 || offset >= buffer.length)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'offset');

if (length < 0 || offset + length > buffer.length)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'length');
validateOffsetLengthRead(offset, length, buffer.length);

if (!isUint32(position))
position = -1;
Expand Down Expand Up @@ -779,11 +790,7 @@ fs.readSync = function(fd, buffer, offset, length, position) {
return 0;
}

if (offset < 0 || offset >= buffer.length)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'offset');

if (length < 0 || offset + length > buffer.length)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'length');
validateOffsetLengthRead(offset, length, buffer.length);

if (!isUint32(position))
position = -1;
Expand Down

0 comments on commit fc8c1b1

Please sign in to comment.