Skip to content

Commit fc8c1b1

Browse files
maclover7joyeecheung
authored andcommitted
fs: extract out validateOffsetLengthRead function
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>
1 parent b9b8294 commit fc8c1b1

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

lib/fs.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,21 @@ function validateFd(fd) {
165165
}
166166
}
167167

168+
function validateOffsetLengthRead(offset, length, bufferLength) {
169+
let err;
170+
171+
if (offset < 0 || offset >= bufferLength) {
172+
err = new errors.RangeError('ERR_OUT_OF_RANGE', 'offset');
173+
} else if (length < 0 || offset + length > bufferLength) {
174+
err = new errors.RangeError('ERR_OUT_OF_RANGE', 'length');
175+
}
176+
177+
if (err !== undefined) {
178+
Error.captureStackTrace(err, validateOffsetLengthRead);
179+
throw err;
180+
}
181+
}
182+
168183
// Special case of `makeCallback()` that is specific to async `*stat()` calls as
169184
// an optimization, since the data passed back to the callback needs to be
170185
// transformed anyway.
@@ -743,11 +758,7 @@ fs.read = function(fd, buffer, offset, length, position, callback) {
743758
});
744759
}
745760

746-
if (offset < 0 || offset >= buffer.length)
747-
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'offset');
748-
749-
if (length < 0 || offset + length > buffer.length)
750-
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'length');
761+
validateOffsetLengthRead(offset, length, buffer.length);
751762

752763
if (!isUint32(position))
753764
position = -1;
@@ -779,11 +790,7 @@ fs.readSync = function(fd, buffer, offset, length, position) {
779790
return 0;
780791
}
781792

782-
if (offset < 0 || offset >= buffer.length)
783-
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'offset');
784-
785-
if (length < 0 || offset + length > buffer.length)
786-
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'length');
793+
validateOffsetLengthRead(offset, length, buffer.length);
787794

788795
if (!isUint32(position))
789796
position = -1;

0 commit comments

Comments
 (0)