Skip to content

Commit

Permalink
fs: extract out validateBuffer 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 fc8c1b1 commit 8983405
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ function makeCallback(cb) {
};
}

function validateBuffer(buffer) {
if (!isUint8Array(buffer)) {
const err = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer',
['Buffer', 'Uint8Array']);
Error.captureStackTrace(err, validateBuffer);
throw err;
}
}

function validateFd(fd) {
let err;

Expand Down Expand Up @@ -745,9 +754,7 @@ fs.openSync = function(path, flags, mode) {

fs.read = function(fd, buffer, offset, length, position, callback) {
validateFd(fd);
if (!isUint8Array(buffer))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer',
['Buffer', 'Uint8Array']);
validateBuffer(buffer);

offset |= 0;
length |= 0;
Expand Down Expand Up @@ -779,9 +786,7 @@ Object.defineProperty(fs.read, internalUtil.customPromisifyArgs,

fs.readSync = function(fd, buffer, offset, length, position) {
validateFd(fd);
if (!isUint8Array(buffer))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer',
['Buffer', 'Uint8Array']);
validateBuffer(buffer);

offset |= 0;
length |= 0;
Expand Down

0 comments on commit 8983405

Please sign in to comment.