1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:57:44 +00:00

LibJS: Protect YearFromTime against non-finite times

This commit is contained in:
Timothy Flynn 2022-01-15 17:37:36 -05:00 committed by Andreas Kling
parent 8ad043fe5e
commit 9be0a0fd28

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/NumericLimits.h>
#include <AK/StringBuilder.h>
#include <AK/Time.h>
#include <LibCore/DateTime.h>
@ -147,6 +148,8 @@ double time_from_year(i32 y)
i32 year_from_time(double t)
{
// the largest integral Number y (closest to +∞) such that TimeFromYear(y) ≤ t
if (!Value(t).is_finite_number())
return NumericLimits<i32>::max();
// Approximation using average number of milliseconds per year. We might have to adjust this guess afterwards.
auto year = static_cast<i32>(t / (365.2425 * Date::ms_per_day) + 1970);