-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fs: (+/-)Infinity and NaN invalid unixtimestamp #11919
fs: (+/-)Infinity and NaN invalid unixtimestamp #11919
Conversation
doc/api/fs.md
Outdated
- If the value is `NaN` or `Infinity`, the value will get converted to | ||
`Date.now() / 1000`. | ||
- If the value is `NaN` or `Infinity` (either positive or negative), an Error is | ||
going to be thrown. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nit: I think 'an Error will be thrown' sounds better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Might be better to list NaN
, Infinity
, or -Infinity
too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, gonna change it
Not sure about this. /cc @jasnell, do we want to put this through a deprecation cycle? @lucamaraschi to me it is not clear why this change should be done — that's not mentioned in this PR. |
lib/fs.js
Outdated
@@ -1164,8 +1164,8 @@ function toUnixTimestamp(time) { | |||
if (typeof time === 'string' && +time == time) { | |||
return +time; | |||
} | |||
if (typeof time === 'number') { | |||
if (!Number.isFinite(time) || time < 0) { | |||
if (typeof time === 'number' && !isNaN(time) && isFinite(time)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isFinite()
covers NaN
, so I think the !isNaN(time)
can be dropped.
'use strict'; | ||
const fs = require('fs'); | ||
const assert = require('assert'); | ||
require('../common'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this up before the other require()
s.
doc/api/fs.md
Outdated
- If the value is `NaN` or `Infinity`, the value will get converted to | ||
`Date.now() / 1000`. | ||
- If the value is `NaN` or `Infinity` (either positive or negative), an Error is | ||
going to be thrown. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Might be better to list NaN
, Infinity
, or -Infinity
too.
lib/fs.js
Outdated
@@ -1164,8 +1164,8 @@ function toUnixTimestamp(time) { | |||
if (typeof time === 'string' && +time == time) { | |||
return +time; | |||
} | |||
if (typeof time === 'number') { | |||
if (!Number.isFinite(time) || time < 0) { | |||
if (typeof time === 'number' && !isNaN(time) && isFinite(time)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be simplified to just typeof time === 'number' && Number.isFinite(time)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Number.isFinite(time)
would cover typeof time === 'number'
anyway, so this is equivalent to just Number.isFinite(time)
.
}); | ||
|
||
const okInputs = [1, -1, '1', '-1', Date.now()]; | ||
okInputs.forEach((input) => assert.doesNotThrow(() => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think using braces and better indentation for the forEach()
callback will make this look better (especially since assert
methods do not return meaningful values). Example:
okInputs.forEach((input) => {
assert.doesNotThrow(() => fs._toUnixTimestamp(input));
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed @mscdex, I was trying to keep it as compact as possible tho.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO compactness isn't as important in tests.
I think this makes sense, silently converting these values to the current time is not something I’d expect as a user (at least not for ±∞).
Hm, is there any reason not to? |
I'm not convinced this behavior was intentional. It could be viewed as a bug fix and it is in a |
const fs = require('fs'); | ||
const assert = require('assert'); | ||
|
||
[Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY, NaN].forEach((input) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: any reasons the shorter form -Infinity
and Infinity
are not used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM once @not-an-aardvark's nit is addressed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I don't think it was intentional in the first place.
@lucamaraschi this needs a rebase. Could you squash the commits while you're at it. |
e5718ba
to
3588f6a
Compare
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.
3588f6a
to
1716ef5
Compare
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 <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Landed in eed87b1 |
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.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
fs