Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
lucamaraschi committed Mar 20, 2017
commit 1716ef515127d65fcef7aec7ed6676d1859eec5f
3 changes: 1 addition & 2 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions test/parallel/test-fs-timestamp-parsing-error.js
Original file line number Diff line number Diff line change
@@ -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));
});
10 changes: 4 additions & 6 deletions test/parallel/test-fs-utimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}));
});
});
});
Expand Down