Closed Bug 811756 Opened 12 years ago Closed 12 years ago

Unchecked cast from NaN double to int in jsdate.cpp

Categories

(Core :: JavaScript Engine, defect)

All
Linux
defect
Not set
critical

Tracking

()

RESOLVED FIXED
mozilla20

People

(Reporter: decoder, Unassigned)

Details

(Keywords: sec-want, Whiteboard: [-fsanitize=float-cast-overflow])

Attachments

(1 file)

Attached patch PatchSplinter Review
There is an unchecked conversion from NaN double to int in js/src/jsdate.cpp:

> inline int
> DaysInFebruary(int year) {
...

> static double
> DateFromTime(double t)
> {
>     double year = YearFromTime(t);
>     double d = DayWithinYear(t, year);
> 
>     int next;
>     if (d <= (next = 30))
>         return d + 1;
>     int step = next;
>     if (d <= (next += DaysInFebruary(year))) // <<<--- year can be NaN here


I believe that the reason is a missing check in the beginning of the method DateFromTime, that verifies that t is indeed finite, just like it is done in MonthFromTime.

However, in addition, DaysInFebruary should probably take a double and not int, since that would be converting to int and immediately back to double (because DaysInFebruary immediately calls another function with the year, which again takes double).

The attached patch fixes both.
Attachment #681512 - Flags: review?(jwalden+bmo)
Is there a test?

More importantly, is the year of our Lord NaN a leap year or not??
The test was this:

date = new Date(NaN).setMonth();

No way to detect this though from inside JS (I think).
In the year NaN, the month of February will have NaN days.
Does that make it a leap year? ^_^
I think that makes it NotAYear.
Comment on attachment 681512 [details] [diff] [review]
Patch

Review of attachment 681512 [details] [diff] [review]:
-----------------------------------------------------------------

rs=me
Attachment #681512 - Flags: review?(jwalden+bmo) → review+
https://hg.mozilla.org/mozilla-central/rev/006bc34d21fd
Status: NEW → RESOLVED
Closed: 12 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla20
Whiteboard: [-fsanitize=float-cast-overflow]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: