Skip to content

Commit

Permalink
fs: move type checking for fs.ftruncate to js
Browse files Browse the repository at this point in the history
PR-URL: #17334
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
jasnell committed Dec 13, 2017
1 parent 8cb080c commit d453fac
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 20 deletions.
8 changes: 4 additions & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -864,11 +864,11 @@ fs.ftruncate = function(fd, len, callback) {
} else if (len === undefined) {
len = 0;
}
if (typeof fd !== 'number')
if (!Number.isInteger(fd))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
if (fd < 0 || fd > 0xFFFFFFFF)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
if (typeof len !== 'number')
if (!Number.isInteger(len))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'number');
len = Math.max(0, len);
const req = new FSReqWrap();
Expand All @@ -880,11 +880,11 @@ fs.ftruncateSync = function(fd, len) {
if (len === undefined) {
len = 0;
}
if (typeof fd !== 'number')
if (!Number.isInteger(fd))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
if (fd < 0 || fd > 0xFFFFFFFF)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
if (typeof len !== 'number')
if (!Number.isInteger(len))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'number');
len = Math.max(0, len);
return binding.ftruncate(fd, len);
Expand Down
19 changes: 3 additions & 16 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -763,24 +763,11 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
static void FTruncate(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 2)
return TYPE_ERROR("fd and length are required");
if (!args[0]->IsInt32())
return TYPE_ERROR("fd must be a file descriptor");
CHECK(args[0]->IsInt32());
CHECK(args[1]->IsNumber());

int fd = args[0]->Int32Value();

// FIXME(bnoordhuis) It's questionable to reject non-ints here but still
// allow implicit coercion from null or undefined to zero. Probably best
// handled in lib/fs.js.
Local<Value> len_v(args[1]);
if (!len_v->IsUndefined() &&
!len_v->IsNull() &&
!IsInt64(len_v->NumberValue())) {
return env->ThrowTypeError("Not an integer");
}

const int64_t len = len_v->IntegerValue();
const int64_t len = args[1]->IntegerValue();

if (args[2]->IsObject()) {
ASYNC_CALL(ftruncate, args[2], UTF8, fd, len)
Expand Down
61 changes: 61 additions & 0 deletions test/parallel/test-fs-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,69 @@ function testFtruncate(cb) {
fs.writeFileSync(file5, 'Hi');
const fd = fs.openSync(file5, 'r+');
process.on('exit', () => fs.closeSync(fd));

['', false, null, {}, []].forEach((i) => {
common.expectsError(
() => fs.ftruncate(fd, i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "len" argument must be of type number'
}
);
});

fs.ftruncate(fd, undefined, common.mustCall(function(err) {
assert.ifError(err);
assert(fs.readFileSync(file5).equals(Buffer.from('')));
}));
}

{
const file6 = path.resolve(tmp, 'truncate-file-6.txt');
fs.writeFileSync(file6, 'Hi');
const fd = fs.openSync(file6, 'r+');
process.on('exit', () => fs.closeSync(fd));
fs.ftruncate(fd, -1, common.mustCall(function(err) {
assert.ifError(err);
assert(fs.readFileSync(file6).equals(Buffer.from('')));
}));
}

['', false, null, undefined, {}, []].forEach((i) => {
common.expectsError(
() => fs.ftruncate(i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type number'
}
);
common.expectsError(
() => fs.ftruncateSync(i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type number'
}
);
});

[-1, 0xFFFFFFFF + 1].forEach((i) => {
common.expectsError(
() => fs.ftruncate(i),
{
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'The "fd" argument is out of range'
}
);
common.expectsError(
() => fs.ftruncateSync(i),
{
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'The "fd" argument is out of range'
}
);
});

0 comments on commit d453fac

Please sign in to comment.