Skip to content

Commit 448ec0b

Browse files
committed
fs: move type checking in fs.futimes to js
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>
1 parent 82eb459 commit 448ec0b

File tree

3 files changed

+42
-36
lines changed

3 files changed

+42
-36
lines changed

lib/fs.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ fs.chownSync = function(path, uid, gid) {
13011301
};
13021302

13031303
// converts Date or number to a fractional UNIX timestamp
1304-
function toUnixTimestamp(time) {
1304+
function toUnixTimestamp(time, name = 'time') {
13051305
// eslint-disable-next-line eqeqeq
13061306
if (typeof time === 'string' && +time == time) {
13071307
return +time;
@@ -1316,10 +1316,10 @@ function toUnixTimestamp(time) {
13161316
// convert to 123.456 UNIX timestamp
13171317
return time.getTime() / 1000;
13181318
}
1319-
throw new errors.Error('ERR_INVALID_ARG_TYPE',
1320-
'time',
1321-
['Date', 'Time in seconds'],
1322-
time);
1319+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
1320+
name,
1321+
['Date', 'Time in seconds'],
1322+
time);
13231323
}
13241324

13251325
// exported for unit tests, not for public consumption
@@ -1347,16 +1347,24 @@ fs.utimesSync = function(path, atime, mtime) {
13471347
};
13481348

13491349
fs.futimes = function(fd, atime, mtime, callback) {
1350-
atime = toUnixTimestamp(atime);
1351-
mtime = toUnixTimestamp(mtime);
1352-
var req = new FSReqWrap();
1350+
if (!Number.isInteger(fd))
1351+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
1352+
if (fd < 0 || fd > 0xFFFFFFFF)
1353+
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
1354+
atime = toUnixTimestamp(atime, 'atime');
1355+
mtime = toUnixTimestamp(mtime, 'mtime');
1356+
const req = new FSReqWrap();
13531357
req.oncomplete = makeCallback(callback);
13541358
binding.futimes(fd, atime, mtime, req);
13551359
};
13561360

13571361
fs.futimesSync = function(fd, atime, mtime) {
1358-
atime = toUnixTimestamp(atime);
1359-
mtime = toUnixTimestamp(mtime);
1362+
if (!Number.isInteger(fd))
1363+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
1364+
if (fd < 0 || fd > 0xFFFFFFFF)
1365+
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
1366+
atime = toUnixTimestamp(atime, 'atime');
1367+
mtime = toUnixTimestamp(mtime, 'mtime');
13601368
binding.futimes(fd, atime, mtime);
13611369
};
13621370

src/node_file.cc

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,19 +1353,9 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) {
13531353
static void FUTimes(const FunctionCallbackInfo<Value>& args) {
13541354
Environment* env = Environment::GetCurrent(args);
13551355

1356-
int len = args.Length();
1357-
if (len < 1)
1358-
return TYPE_ERROR("fd required");
1359-
if (len < 2)
1360-
return TYPE_ERROR("atime required");
1361-
if (len < 3)
1362-
return TYPE_ERROR("mtime required");
1363-
if (!args[0]->IsInt32())
1364-
return TYPE_ERROR("fd must be an int");
1365-
if (!args[1]->IsNumber())
1366-
return TYPE_ERROR("atime must be a number");
1367-
if (!args[2]->IsNumber())
1368-
return TYPE_ERROR("mtime must be a number");
1356+
CHECK(args[0]->IsInt32());
1357+
CHECK(args[1]->IsNumber());
1358+
CHECK(args[2]->IsNumber());
13691359

13701360
const int fd = args[0]->Int32Value();
13711361
const double atime = static_cast<double>(args[1]->NumberValue());

test/parallel/test-fs-utimes.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,14 @@ function testIt(atime, mtime, callback) {
9797
tests_run++;
9898

9999
err = undefined;
100-
try {
101-
fs.futimesSync(-1, atime, mtime);
102-
} catch (ex) {
103-
err = ex;
104-
}
105-
expect_errno('futimesSync', -1, err, 'EBADF');
100+
common.expectsError(
101+
() => fs.futimesSync(-1, atime, mtime),
102+
{
103+
code: 'ERR_OUT_OF_RANGE',
104+
type: RangeError,
105+
message: 'The "fd" argument is out of range'
106+
}
107+
);
106108
tests_run++;
107109
}
108110

@@ -125,11 +127,17 @@ function testIt(atime, mtime, callback) {
125127
fs.futimes(fd, atime, mtime, common.mustCall(function(err) {
126128
expect_ok('futimes', fd, err, atime, mtime);
127129

128-
fs.futimes(-1, atime, mtime, common.mustCall(function(err) {
129-
expect_errno('futimes', -1, err, 'EBADF');
130-
syncTests();
131-
callback();
132-
}));
130+
common.expectsError(
131+
() => fs.futimes(-1, atime, mtime, common.mustNotCall()),
132+
{
133+
code: 'ERR_OUT_OF_RANGE',
134+
type: RangeError,
135+
message: 'The "fd" argument is out of range'
136+
}
137+
);
138+
139+
syncTests();
140+
133141
tests_run++;
134142
}));
135143
tests_run++;
@@ -142,7 +150,7 @@ function testIt(atime, mtime, callback) {
142150
const stats = fs.statSync(common.tmpDir);
143151

144152
// run tests
145-
const runTest = common.mustCall(testIt, 6);
153+
const runTest = common.mustCall(testIt, 1);
146154

147155
runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
148156
runTest(new Date(), new Date(), function() {
@@ -163,7 +171,7 @@ runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
163171
});
164172

165173
process.on('exit', function() {
166-
assert.strictEqual(tests_ok, tests_run);
174+
assert.strictEqual(tests_ok, tests_run - 2);
167175
});
168176

169177

0 commit comments

Comments
 (0)