Permalink
Browse files

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>
  • Loading branch information...
lucamaraschi authored and jasnell committed Mar 17, 2017
1 parent ae8a869 commit eed87b1637d64340856bb7f77db899f5283ca84f
Showing with 11 additions and 13 deletions.
  1. +1 −2 doc/api/fs.md
  2. +2 −2 lib/fs.js
  3. +4 −3 test/parallel/test-fs-timestamp-parsing-error.js
  4. +4 −6 test/parallel/test-fs-utimes.js
View
@@ -1880,8 +1880,7 @@ follow these rules:
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
converted to the corresponding number.
- If the value is `NaN` or `Infinity`, the value will get converted to
`Date.now() / 1000`.
- If the value is `NaN`, `Infinity` or `-Infinity`, an Error will be thrown.
## fs.utimesSync(path, atime, mtime)
<!-- YAML
View
@@ -1165,8 +1165,8 @@ function toUnixTimestamp(time) {
if (typeof time === 'string' && +time == time) {
return +time;
}
if (typeof time === 'number') {
if (!Number.isFinite(time) || time < 0) {
if (Number.isFinite(time)) {
if (time < 0) {
return Date.now() / 1000;
}
return time;
@@ -1,16 +1,17 @@
'use strict';
require('../common');
const assert = require('assert');
const fs = require('fs');
const assert = require('assert');
[undefined, null, []].forEach((input) => {
[Infinity, -Infinity, NaN].forEach((input) => {
assert.throws(() => fs._toUnixTimestamp(input),
new RegExp('^Error: Cannot parse time: ' + input + '$'));
});
assert.throws(() => fs._toUnixTimestamp({}),
/^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));
});
@@ -143,17 +143,15 @@ function testIt(atime, mtime, callback) {
const stats = fs.statSync(__filename);
// 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(), new Date(), function() {
runTest(123456.789, 123456.789, function() {
runTest(stats.mtime, stats.mtime, function() {
runTest(NaN, Infinity, function() {
runTest('123456', -1, common.mustCall(function() {
// done
}));
});
runTest('123456', -1, common.mustCall(function() {
// done
}));
});
});
});

0 comments on commit eed87b1

Please sign in to comment.