fs: extract out validateOffsetLengthWrite 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
8983405508
commit
300ea7396f
27
lib/fs.js
27
lib/fs.js
@ -189,6 +189,21 @@ function validateOffsetLengthRead(offset, length, bufferLength) {
|
||||
}
|
||||
}
|
||||
|
||||
function validateOffsetLengthWrite(offset, length, byteLength) {
|
||||
let err;
|
||||
|
||||
if (offset > byteLength) {
|
||||
err = new errors.RangeError('ERR_OUT_OF_RANGE', 'offset');
|
||||
} else if (offset + length > byteLength || offset + length > kMaxLength) {
|
||||
err = new errors.RangeError('ERR_OUT_OF_RANGE', 'length');
|
||||
}
|
||||
|
||||
if (err !== undefined) {
|
||||
Error.captureStackTrace(err, validateOffsetLengthWrite);
|
||||
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.
|
||||
@ -826,11 +841,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
|
||||
length = buffer.length - offset;
|
||||
if (typeof position !== 'number')
|
||||
position = null;
|
||||
const byteLength = buffer.byteLength;
|
||||
if (offset > byteLength)
|
||||
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'offset');
|
||||
if (offset + length > byteLength || offset + length > kMaxLength)
|
||||
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'length');
|
||||
validateOffsetLengthWrite(offset, length, buffer.byteLength);
|
||||
return binding.writeBuffer(fd, buffer, offset, length, position, req);
|
||||
}
|
||||
|
||||
@ -865,11 +876,7 @@ fs.writeSync = function(fd, buffer, offset, length, position) {
|
||||
offset = 0;
|
||||
if (typeof length !== 'number')
|
||||
length = buffer.length - offset;
|
||||
const byteLength = buffer.byteLength;
|
||||
if (offset > byteLength)
|
||||
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'offset');
|
||||
if (offset + length > byteLength || offset + length > kMaxLength)
|
||||
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'length');
|
||||
validateOffsetLengthWrite(offset, length, buffer.byteLength);
|
||||
return binding.writeBuffer(fd, buffer, offset, length, position);
|
||||
}
|
||||
if (typeof buffer !== 'string')
|
||||
|
Loading…
x
Reference in New Issue
Block a user