fs: add length validation to fs.truncate()
This commit adds validation to the length parameter of fs.truncate(). Prior to this commit, passing a non-number would trigger a CHECK() in the binding layer. PR-URL: https://github.com/nodejs/node/pull/20851 Fixes: https://github.com/nodejs/node/issues/20844 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
parent
f8464c8698
commit
751a42a30f
@ -78,6 +78,7 @@ const {
|
||||
const {
|
||||
isUint32,
|
||||
validateAndMaskMode,
|
||||
validateInteger,
|
||||
validateInt32,
|
||||
validateUint32
|
||||
} = require('internal/validators');
|
||||
@ -609,6 +610,7 @@ function truncate(path, len, callback) {
|
||||
len = 0;
|
||||
}
|
||||
|
||||
validateInteger(len, 'len');
|
||||
callback = maybeCallback(callback);
|
||||
fs.open(path, 'r+', function(er, fd) {
|
||||
if (er) return callback(er);
|
||||
|
@ -179,6 +179,16 @@ function testFtruncate(cb) {
|
||||
process.on('exit', () => fs.closeSync(fd));
|
||||
|
||||
['', false, null, {}, []].forEach((input) => {
|
||||
assert.throws(
|
||||
() => fs.truncate(file5, input, common.mustNotCall()),
|
||||
{
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
|
||||
message: 'The "len" argument must be of type number. ' +
|
||||
`Received type ${typeof input}`
|
||||
}
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => fs.ftruncate(fd, input),
|
||||
{
|
||||
@ -191,6 +201,16 @@ function testFtruncate(cb) {
|
||||
});
|
||||
|
||||
[-1.5, 1.5].forEach((input) => {
|
||||
assert.throws(
|
||||
() => fs.truncate(file5, input),
|
||||
{
|
||||
code: 'ERR_OUT_OF_RANGE',
|
||||
name: 'RangeError [ERR_OUT_OF_RANGE]',
|
||||
message: 'The value of "len" is out of range. It must be ' +
|
||||
`an integer. Received ${input}`
|
||||
}
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => fs.ftruncate(fd, input),
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user