Skip to content

Commit eed87b1

Browse files
lucamaraschijasnell
authored andcommitted
fs: (+/-)Infinity and NaN invalid unixtimestamp
Infinity and NaN are currently considered valid input when generating a unix time stamp but are defaulted arbitrarly to Date.now()/1000. This PR removes this behaviour and throw an exception like all the other invalid input types. PR-URL: #11919 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ae8a869 commit eed87b1

File tree

4 files changed

+11
-13
lines changed

4 files changed

+11
-13
lines changed

doc/api/fs.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1880,8 +1880,7 @@ follow these rules:
18801880
returns milliseconds, so it should be divided by 1000 before passing it in.
18811881
- If the value is a numeric string like `'123456789'`, the value will get
18821882
converted to the corresponding number.
1883-
- If the value is `NaN` or `Infinity`, the value will get converted to
1884-
`Date.now() / 1000`.
1883+
- If the value is `NaN`, `Infinity` or `-Infinity`, an Error will be thrown.
18851884

18861885
## fs.utimesSync(path, atime, mtime)
18871886
<!-- YAML

lib/fs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1165,8 +1165,8 @@ function toUnixTimestamp(time) {
11651165
if (typeof time === 'string' && +time == time) {
11661166
return +time;
11671167
}
1168-
if (typeof time === 'number') {
1169-
if (!Number.isFinite(time) || time < 0) {
1168+
if (Number.isFinite(time)) {
1169+
if (time < 0) {
11701170
return Date.now() / 1000;
11711171
}
11721172
return time;
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
'use strict';
22
require('../common');
3-
const assert = require('assert');
43
const fs = require('fs');
4+
const assert = require('assert');
55

6-
[undefined, null, []].forEach((input) => {
6+
[Infinity, -Infinity, NaN].forEach((input) => {
77
assert.throws(() => fs._toUnixTimestamp(input),
88
new RegExp('^Error: Cannot parse time: ' + input + '$'));
99
});
1010

1111
assert.throws(() => fs._toUnixTimestamp({}),
1212
/^Error: Cannot parse time: \[object Object\]$/);
1313

14-
[1, '1', Date.now(), -1, '-1', Infinity].forEach((input) => {
14+
const okInputs = [1, -1, '1', '-1', Date.now()];
15+
okInputs.forEach((input) => {
1516
assert.doesNotThrow(() => fs._toUnixTimestamp(input));
1617
});

test/parallel/test-fs-utimes.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,15 @@ function testIt(atime, mtime, callback) {
143143
const stats = fs.statSync(__filename);
144144

145145
// run tests
146-
const runTest = common.mustCall(testIt, 6);
146+
const runTest = common.mustCall(testIt, 5);
147147

148148
runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
149149
runTest(new Date(), new Date(), function() {
150150
runTest(123456.789, 123456.789, function() {
151151
runTest(stats.mtime, stats.mtime, function() {
152-
runTest(NaN, Infinity, function() {
153-
runTest('123456', -1, common.mustCall(function() {
154-
// done
155-
}));
156-
});
152+
runTest('123456', -1, common.mustCall(function() {
153+
// done
154+
}));
157155
});
158156
});
159157
});

0 commit comments

Comments
 (0)