fs: extract out validateOffsetLengthRead function
PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
b9b8294dda
commit
fc8c1b1ded
27
lib/fs.js
27
lib/fs.js
@ -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
|
// 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
|
// an optimization, since the data passed back to the callback needs to be
|
||||||
// transformed anyway.
|
// transformed anyway.
|
||||||
@ -743,11 +758,7 @@ fs.read = function(fd, buffer, offset, length, position, callback) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (offset < 0 || offset >= buffer.length)
|
validateOffsetLengthRead(offset, length, 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');
|
|
||||||
|
|
||||||
if (!isUint32(position))
|
if (!isUint32(position))
|
||||||
position = -1;
|
position = -1;
|
||||||
@ -779,11 +790,7 @@ fs.readSync = function(fd, buffer, offset, length, position) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (offset < 0 || offset >= buffer.length)
|
validateOffsetLengthRead(offset, length, 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');
|
|
||||||
|
|
||||||
if (!isUint32(position))
|
if (!isUint32(position))
|
||||||
position = -1;
|
position = -1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user