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: https://github.com/nodejs/node/pull/11919 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
ae8a8691e6
commit
eed87b1637
@ -1880,8 +1880,7 @@ follow these rules:
|
|||||||
returns milliseconds, so it should be divided by 1000 before passing it in.
|
returns milliseconds, so it should be divided by 1000 before passing it in.
|
||||||
- If the value is a numeric string like `'123456789'`, the value will get
|
- If the value is a numeric string like `'123456789'`, the value will get
|
||||||
converted to the corresponding number.
|
converted to the corresponding number.
|
||||||
- If the value is `NaN` or `Infinity`, the value will get converted to
|
- If the value is `NaN`, `Infinity` or `-Infinity`, an Error will be thrown.
|
||||||
`Date.now() / 1000`.
|
|
||||||
|
|
||||||
## fs.utimesSync(path, atime, mtime)
|
## fs.utimesSync(path, atime, mtime)
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
|
@ -1165,8 +1165,8 @@ function toUnixTimestamp(time) {
|
|||||||
if (typeof time === 'string' && +time == time) {
|
if (typeof time === 'string' && +time == time) {
|
||||||
return +time;
|
return +time;
|
||||||
}
|
}
|
||||||
if (typeof time === 'number') {
|
if (Number.isFinite(time)) {
|
||||||
if (!Number.isFinite(time) || time < 0) {
|
if (time < 0) {
|
||||||
return Date.now() / 1000;
|
return Date.now() / 1000;
|
||||||
}
|
}
|
||||||
return time;
|
return time;
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
require('../common');
|
||||||
const assert = require('assert');
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const assert = require('assert');
|
||||||
|
|
||||||
[undefined, null, []].forEach((input) => {
|
[Infinity, -Infinity, NaN].forEach((input) => {
|
||||||
assert.throws(() => fs._toUnixTimestamp(input),
|
assert.throws(() => fs._toUnixTimestamp(input),
|
||||||
new RegExp('^Error: Cannot parse time: ' + input + '$'));
|
new RegExp('^Error: Cannot parse time: ' + input + '$'));
|
||||||
});
|
});
|
||||||
@ -11,6 +11,7 @@ const fs = require('fs');
|
|||||||
assert.throws(() => fs._toUnixTimestamp({}),
|
assert.throws(() => fs._toUnixTimestamp({}),
|
||||||
/^Error: Cannot parse time: \[object Object\]$/);
|
/^Error: Cannot parse time: \[object Object\]$/);
|
||||||
|
|
||||||
[1, '1', Date.now(), -1, '-1', Infinity].forEach((input) => {
|
const okInputs = [1, -1, '1', '-1', Date.now()];
|
||||||
|
okInputs.forEach((input) => {
|
||||||
assert.doesNotThrow(() => fs._toUnixTimestamp(input));
|
assert.doesNotThrow(() => fs._toUnixTimestamp(input));
|
||||||
});
|
});
|
||||||
|
@ -143,13 +143,12 @@ function testIt(atime, mtime, callback) {
|
|||||||
const stats = fs.statSync(__filename);
|
const stats = fs.statSync(__filename);
|
||||||
|
|
||||||
// run tests
|
// run tests
|
||||||
const runTest = common.mustCall(testIt, 6);
|
const runTest = common.mustCall(testIt, 5);
|
||||||
|
|
||||||
runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
|
runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
|
||||||
runTest(new Date(), new Date(), function() {
|
runTest(new Date(), new Date(), function() {
|
||||||
runTest(123456.789, 123456.789, function() {
|
runTest(123456.789, 123456.789, function() {
|
||||||
runTest(stats.mtime, stats.mtime, function() {
|
runTest(stats.mtime, stats.mtime, function() {
|
||||||
runTest(NaN, Infinity, function() {
|
|
||||||
runTest('123456', -1, common.mustCall(function() {
|
runTest('123456', -1, common.mustCall(function() {
|
||||||
// done
|
// done
|
||||||
}));
|
}));
|
||||||
@ -157,7 +156,6 @@ runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
process.on('exit', function() {
|
process.on('exit', function() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user