Skip to content

Commit db340ae

Browse files
linusgawesomekling
authored andcommitted
LibJS: Add missing exception check in Date() constructor
1 parent ddbd88d commit db340ae

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

Userland/Libraries/LibJS/Runtime/DateConstructor.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,29 +168,33 @@ Value DateConstructor::call()
168168

169169
Value DateConstructor::construct(Function&)
170170
{
171-
if (vm().argument_count() == 0) {
171+
auto& vm = this->vm();
172+
if (vm.argument_count() == 0) {
172173
struct timeval tv;
173174
gettimeofday(&tv, nullptr);
174175
auto datetime = Core::DateTime::now();
175176
auto milliseconds = static_cast<u16>(tv.tv_usec / 1000);
176177
return Date::create(global_object(), datetime, milliseconds);
177178
}
178-
if (vm().argument_count() == 1) {
179-
auto value = vm().argument(0);
179+
if (vm.argument_count() == 1) {
180+
auto value = vm.argument(0);
180181
if (value.is_string())
181182
value = parse_simplified_iso8601(value.as_string().string());
182183
// A timestamp since the epoch, in UTC.
184+
// FIXME: This doesn't construct an "Invalid Date" object if the argument is NaN.
183185
// FIXME: Date() probably should use a double as internal representation, so that NaN arguments and larger offsets are handled correctly.
184186
double value_as_double = value.to_double(global_object());
187+
if (vm.exception())
188+
return {};
185189
auto datetime = Core::DateTime::from_timestamp(static_cast<time_t>(value_as_double / 1000));
186190
auto milliseconds = static_cast<u16>(fmod(value_as_double, 1000));
187191
return Date::create(global_object(), datetime, milliseconds);
188192
}
189193
// A date/time in components, in local time.
190-
// FIXME: This doesn't construct an "Invalid Date" object if one of the parameters is NaN.
191-
auto arg_or = [this](size_t i, i32 fallback) { return vm().argument_count() > i ? vm().argument(i).to_i32(global_object()) : fallback; };
192-
int year = vm().argument(0).to_i32(global_object());
193-
int month_index = vm().argument(1).to_i32(global_object());
194+
// FIXME: This doesn't construct an "Invalid Date" object if one of the arguments is NaN.
195+
auto arg_or = [this, &vm](size_t i, i32 fallback) { return vm.argument_count() > i ? vm.argument(i).to_i32(global_object()) : fallback; };
196+
int year = vm.argument(0).to_i32(global_object());
197+
int month_index = vm.argument(1).to_i32(global_object());
194198
int day = arg_or(2, 1);
195199
int hours = arg_or(3, 0);
196200
int minutes = arg_or(4, 0);

0 commit comments

Comments
 (0)