Skip to content

Commit b5da46f

Browse files
committed
Fix segmentation fault in Date helper functions
JerryScript-DCO-1.0-Signed-off-by: Szilard Ledan szledan.u-szeged@partner.samsung.com
1 parent e34ab90 commit b5da46f

File tree

2 files changed

+158
-108
lines changed

2 files changed

+158
-108
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-date-prototype.cpp

Lines changed: 124 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -561,27 +561,31 @@ ecma_builtin_date_prototype_set_minutes (ecma_value_t this_arg, /**< this argume
561561
ECMA_TRY_CATCH (this_time_value, ecma_builtin_date_prototype_get_time (this_arg), ret_value);
562562
ecma_number_t t = ecma_date_local_time (*ecma_get_number_from_value (this_time_value));
563563

564-
/* 2. Let 'm' be ToNumber('min') where 'min' is args[0]. */
565-
ECMA_OP_TO_NUMBER_TRY_CATCH (m, args[0], ret_value);
566-
567-
/* 3. If 'sec' is not specified, then let 's' be SecFromTime('t');
568-
* otherwise, let 's' be ToNumber('sec') where 'sec' is args[1]. */
564+
/* 2. */
565+
ecma_number_t m = ecma_number_make_nan ();
569566
ecma_number_t s = ecma_date_sec_from_time (t);
570567
ecma_number_t milli = ecma_date_ms_from_time (t);
571-
if (args_number > 1 && !ecma_is_value_undefined (args[1]))
568+
if (args_number > 0 && !ecma_is_value_undefined (args[0]))
572569
{
573-
ECMA_OP_TO_NUMBER_TRY_CATCH (sec, args[1], ret_value);
574-
s = sec;
570+
ECMA_OP_TO_NUMBER_TRY_CATCH (min, args[0], ret_value);
571+
m = min;
575572

576-
/* 4. If 'ms' is not specified, then let 'milli' be msFromTime('t');
577-
* otherwise, let 'milli' be ToNumber('ms') where 'ms' is args[2]. */
578-
if (args_number > 2 && !ecma_is_value_undefined (args[2]))
573+
/* 3. */
574+
if (args_number > 1 && !ecma_is_value_undefined (args[1]))
579575
{
580-
ECMA_OP_TO_NUMBER_TRY_CATCH (ms, args[2], ret_value);
581-
milli = ms;
582-
ECMA_OP_TO_NUMBER_FINALIZE (ms);
576+
ECMA_OP_TO_NUMBER_TRY_CATCH (sec, args[1], ret_value);
577+
s = sec;
578+
579+
/* 4. */
580+
if (args_number > 2 && !ecma_is_value_undefined (args[2]))
581+
{
582+
ECMA_OP_TO_NUMBER_TRY_CATCH (ms, args[2], ret_value);
583+
milli = ms;
584+
ECMA_OP_TO_NUMBER_FINALIZE (ms);
585+
}
586+
ECMA_OP_TO_NUMBER_FINALIZE (sec);
583587
}
584-
ECMA_OP_TO_NUMBER_FINALIZE (sec);
588+
ECMA_OP_TO_NUMBER_FINALIZE (min);
585589
}
586590

587591
if (ecma_is_completion_value_empty (ret_value))
@@ -593,7 +597,6 @@ ecma_builtin_date_prototype_set_minutes (ecma_value_t this_arg, /**< this argume
593597
ecma_date_make_time (hour, m, s, milli),
594598
ECMA_DATE_LOCAL);
595599
}
596-
ECMA_OP_TO_NUMBER_FINALIZE (m);
597600
ECMA_FINALIZE (this_time_value);
598601

599602
return ret_value;
@@ -619,27 +622,31 @@ ecma_builtin_date_prototype_set_utc_minutes (ecma_value_t this_arg, /**< this ar
619622
ECMA_TRY_CATCH (this_time_value, ecma_builtin_date_prototype_get_time (this_arg), ret_value);
620623
ecma_number_t t = *ecma_get_number_from_value (this_time_value);
621624

622-
/* 2. Let 'm' be ToNumber('min') where 'min' is args[0]. */
623-
ECMA_OP_TO_NUMBER_TRY_CATCH (m, args[0], ret_value);
624-
625-
/* 3. If 'sec' is not specified, then let 's' be SecFromTime('t');
626-
* otherwise, let 's' be ToNumber('sec') where 'sec' is args[1]. */
625+
/* 2. */
626+
ecma_number_t m = ecma_number_make_nan ();
627627
ecma_number_t s = ecma_date_sec_from_time (t);
628628
ecma_number_t milli = ecma_date_ms_from_time (t);
629-
if (args_number > 1 && !ecma_is_value_undefined (args[1]))
629+
if (args_number > 0 && !ecma_is_value_undefined (args[0]))
630630
{
631-
ECMA_OP_TO_NUMBER_TRY_CATCH (sec, args[1], ret_value);
632-
s = sec;
631+
ECMA_OP_TO_NUMBER_TRY_CATCH (min, args[0], ret_value);
632+
m = min;
633633

634-
/* 4. If 'ms' is not specified, then let 'milli' be msFromTime('t');
635-
* otherwise, let 'milli' be ToNumber('ms') where 'ms' is args[2]. */
636-
if (args_number > 2 && !ecma_is_value_undefined (args[2]))
634+
/* 3. */
635+
if (args_number > 1 && !ecma_is_value_undefined (args[1]))
637636
{
638-
ECMA_OP_TO_NUMBER_TRY_CATCH (ms, args[2], ret_value);
639-
milli = ms;
640-
ECMA_OP_TO_NUMBER_FINALIZE (ms);
637+
ECMA_OP_TO_NUMBER_TRY_CATCH (sec, args[1], ret_value);
638+
s = sec;
639+
640+
/* 4. */
641+
if (args_number > 2 && !ecma_is_value_undefined (args[2]))
642+
{
643+
ECMA_OP_TO_NUMBER_TRY_CATCH (ms, args[2], ret_value);
644+
milli = ms;
645+
ECMA_OP_TO_NUMBER_FINALIZE (ms);
646+
}
647+
ECMA_OP_TO_NUMBER_FINALIZE (sec);
641648
}
642-
ECMA_OP_TO_NUMBER_FINALIZE (sec);
649+
ECMA_OP_TO_NUMBER_FINALIZE (min);
643650
}
644651

645652
if (ecma_is_completion_value_empty (ret_value))
@@ -651,7 +658,6 @@ ecma_builtin_date_prototype_set_utc_minutes (ecma_value_t this_arg, /**< this ar
651658
ecma_date_make_time (hour, m, s, milli),
652659
ECMA_DATE_UTC);
653660
}
654-
ECMA_OP_TO_NUMBER_FINALIZE (m);
655661
ECMA_FINALIZE (this_time_value);
656662

657663
return ret_value;
@@ -677,37 +683,40 @@ ecma_builtin_date_prototype_set_hours (ecma_value_t this_arg, /**< this argument
677683
ECMA_TRY_CATCH (this_time_value, ecma_builtin_date_prototype_get_time (this_arg), ret_value);
678684
ecma_number_t t = ecma_date_local_time (*ecma_get_number_from_value (this_time_value));
679685

680-
/* 2. Let 'h' be ToNumber('hour') where 'hour' is args[0]. */
681-
ECMA_OP_TO_NUMBER_TRY_CATCH (h, args[0], ret_value);
682-
683-
/* 3. If 'min' is not specified, then let 'm' be MinFromTime('t');
684-
* otherwise, let 'm' be ToNumber('min') where 'min' is args[1]. */
686+
/* 2. */
687+
ecma_number_t h = ecma_number_make_nan ();
685688
ecma_number_t m = ecma_date_min_from_time (t);
686689
ecma_number_t s = ecma_date_sec_from_time (t);
687690
ecma_number_t milli = ecma_date_ms_from_time (t);
688-
if (args_number > 1 && !ecma_is_value_undefined (args[1]))
691+
if (args_number > 0 && !ecma_is_value_undefined (args[0]))
689692
{
690-
ECMA_OP_TO_NUMBER_TRY_CATCH (min, args[1], ret_value);
691-
m = min;
693+
ECMA_OP_TO_NUMBER_TRY_CATCH (hour, args[0], ret_value);
694+
h = hour;
692695

693-
/* 4. If 'sec' is not specified, then let 's' be SecFromTime('t');
694-
* otherwise, let 's' be ToNumber('sec') where 'sec' is args[2]. */
695-
if (args_number > 2 && !ecma_is_value_undefined (args[2]))
696+
/* 3. */
697+
if (args_number > 1 && !ecma_is_value_undefined (args[1]))
696698
{
697-
ECMA_OP_TO_NUMBER_TRY_CATCH (sec, args[2], ret_value);
698-
s = sec;
699+
ECMA_OP_TO_NUMBER_TRY_CATCH (min, args[1], ret_value);
700+
m = min;
699701

700-
/* 5. If 'ms' is not specified, then let 'milli' be msFromTime('t');
701-
* otherwise, let 'milli' be ToNumber('ms') where 'ms' is args[3]. */
702-
if (args_number > 3 && !ecma_is_value_undefined (args[3]))
702+
/* 4. */
703+
if (args_number > 2 && !ecma_is_value_undefined (args[2]))
703704
{
704-
ECMA_OP_TO_NUMBER_TRY_CATCH (ms, args[3], ret_value);
705-
milli = ms;
706-
ECMA_OP_TO_NUMBER_FINALIZE (ms);
705+
ECMA_OP_TO_NUMBER_TRY_CATCH (sec, args[2], ret_value);
706+
s = sec;
707+
708+
/* 5. */
709+
if (args_number > 3 && !ecma_is_value_undefined (args[3]))
710+
{
711+
ECMA_OP_TO_NUMBER_TRY_CATCH (ms, args[3], ret_value);
712+
milli = ms;
713+
ECMA_OP_TO_NUMBER_FINALIZE (ms);
714+
}
715+
ECMA_OP_TO_NUMBER_FINALIZE (sec);
707716
}
708-
ECMA_OP_TO_NUMBER_FINALIZE (sec);
717+
ECMA_OP_TO_NUMBER_FINALIZE (min);
709718
}
710-
ECMA_OP_TO_NUMBER_FINALIZE (min);
719+
ECMA_OP_TO_NUMBER_FINALIZE (hour);
711720
}
712721

713722
if (ecma_is_completion_value_empty (ret_value))
@@ -718,7 +727,6 @@ ecma_builtin_date_prototype_set_hours (ecma_value_t this_arg, /**< this argument
718727
ecma_date_make_time (h, m, s, milli),
719728
ECMA_DATE_LOCAL);
720729
}
721-
ECMA_OP_TO_NUMBER_FINALIZE (h);
722730
ECMA_FINALIZE (this_time_value);
723731

724732
return ret_value;
@@ -744,37 +752,40 @@ ecma_builtin_date_prototype_set_utc_hours (ecma_value_t this_arg, /**< this argu
744752
ECMA_TRY_CATCH (this_time_value, ecma_builtin_date_prototype_get_time (this_arg), ret_value);
745753
ecma_number_t t = *ecma_get_number_from_value (this_time_value);
746754

747-
/* 2. Let 'h' be ToNumber('hour') where 'hour' is args[0]. */
748-
ECMA_OP_TO_NUMBER_TRY_CATCH (h, args[0], ret_value);
749-
750-
/* 3. If 'min' is not specified, then let 'm' be MinFromTime('t');
751-
* otherwise, let 'm' be ToNumber('min') where 'min' is args[1]. */
755+
/* 2. */
756+
ecma_number_t h = ecma_number_make_nan ();
752757
ecma_number_t m = ecma_date_min_from_time (t);
753758
ecma_number_t s = ecma_date_sec_from_time (t);
754759
ecma_number_t milli = ecma_date_ms_from_time (t);
755-
if (args_number > 1 && !ecma_is_value_undefined (args[1]))
760+
if (args_number > 0 && !ecma_is_value_undefined (args[0]))
756761
{
757-
ECMA_OP_TO_NUMBER_TRY_CATCH (min, args[1], ret_value);
758-
m = min;
762+
ECMA_OP_TO_NUMBER_TRY_CATCH (hour, args[0], ret_value);
763+
h = hour;
759764

760-
/* 4. If 'sec' is not specified, then let 's' be SecFromTime('t');
761-
* otherwise, let 's' be ToNumber('sec') where 'sec' is args[2]. */
762-
if (args_number > 2 && !ecma_is_value_undefined (args[2]))
765+
/* 3. */
766+
if (args_number > 1 && !ecma_is_value_undefined (args[1]))
763767
{
764-
ECMA_OP_TO_NUMBER_TRY_CATCH (sec, args[2], ret_value);
765-
s = sec;
768+
ECMA_OP_TO_NUMBER_TRY_CATCH (min, args[1], ret_value);
769+
m = min;
766770

767-
/* 5. If 'ms' is not specified, then let 'milli' be msFromTime('t');
768-
* otherwise, let 'milli' be ToNumber('ms') where 'ms' is args[3]. */
769-
if (args_number > 3 && !ecma_is_value_undefined (args[3]))
771+
/* 4. */
772+
if (args_number > 2 && !ecma_is_value_undefined (args[2]))
770773
{
771-
ECMA_OP_TO_NUMBER_TRY_CATCH (ms, args[3], ret_value);
772-
milli = ms;
773-
ECMA_OP_TO_NUMBER_FINALIZE (ms);
774+
ECMA_OP_TO_NUMBER_TRY_CATCH (sec, args[2], ret_value);
775+
s = sec;
776+
777+
/* 5. */
778+
if (args_number > 3 && !ecma_is_value_undefined (args[3]))
779+
{
780+
ECMA_OP_TO_NUMBER_TRY_CATCH (ms, args[3], ret_value);
781+
milli = ms;
782+
ECMA_OP_TO_NUMBER_FINALIZE (ms);
783+
}
784+
ECMA_OP_TO_NUMBER_FINALIZE (sec);
774785
}
775-
ECMA_OP_TO_NUMBER_FINALIZE (sec);
786+
ECMA_OP_TO_NUMBER_FINALIZE (min);
776787
}
777-
ECMA_OP_TO_NUMBER_FINALIZE (min);
788+
ECMA_OP_TO_NUMBER_FINALIZE (hour);
778789
}
779790

780791
if (ecma_is_completion_value_empty (ret_value))
@@ -785,7 +796,6 @@ ecma_builtin_date_prototype_set_utc_hours (ecma_value_t this_arg, /**< this argu
785796
ecma_date_make_time (h, m, s, milli),
786797
ECMA_DATE_UTC);
787798
}
788-
ECMA_OP_TO_NUMBER_FINALIZE (h);
789799
ECMA_FINALIZE (this_time_value);
790800

791801
return ret_value;
@@ -975,27 +985,31 @@ ecma_builtin_date_prototype_set_full_year (ecma_value_t this_arg, /**< this argu
975985
t = ECMA_NUMBER_ZERO;
976986
}
977987

978-
/* 2. Let 'y' be ToNumber('year') where 'year' is args[0]. */
979-
ECMA_OP_TO_NUMBER_TRY_CATCH (y, args[0], ret_value);
980-
981-
/* 3. If 'month' is not specified, then let 'm' be MonthFromTime('t');
982-
* otherwise, let 'm' be ToNumber('month') where 'month' is args[1]. */
988+
/* 2. */
989+
ecma_number_t y = ecma_number_make_nan ();
983990
ecma_number_t m = ecma_date_month_from_time (t);
984991
ecma_number_t dt = ecma_date_date_from_time (t);
985-
if (args_number > 1 && !ecma_is_value_undefined (args[1]))
992+
if (args_number > 0 && !ecma_is_value_undefined (args[0]))
986993
{
987-
ECMA_OP_TO_NUMBER_TRY_CATCH (month, args[1], ret_value);
988-
m = month;
994+
ECMA_OP_TO_NUMBER_TRY_CATCH (year, args[0], ret_value);
995+
y = year;
989996

990-
/* 4. If 'date' is not specified, then let 'dt' be DateFromTime('t');
991-
* otherwise, let 'dt' be ToNumber('date') where 'date' is args[2]. */
992-
if (args_number > 2 && !ecma_is_value_undefined (args[2]))
997+
/* 3. */
998+
if (args_number > 1 && !ecma_is_value_undefined (args[1]))
993999
{
994-
ECMA_OP_TO_NUMBER_TRY_CATCH (date, args[2], ret_value);
995-
dt = date;
996-
ECMA_OP_TO_NUMBER_FINALIZE (date);
1000+
ECMA_OP_TO_NUMBER_TRY_CATCH (month, args[1], ret_value);
1001+
m = month;
1002+
1003+
/* 4. */
1004+
if (args_number > 2 && !ecma_is_value_undefined (args[2]))
1005+
{
1006+
ECMA_OP_TO_NUMBER_TRY_CATCH (date, args[2], ret_value);
1007+
dt = date;
1008+
ECMA_OP_TO_NUMBER_FINALIZE (date);
1009+
}
1010+
ECMA_OP_TO_NUMBER_FINALIZE (month);
9971011
}
998-
ECMA_OP_TO_NUMBER_FINALIZE (month);
1012+
ECMA_OP_TO_NUMBER_FINALIZE (year);
9991013
}
10001014

10011015
if (ecma_is_completion_value_empty (ret_value))
@@ -1006,7 +1020,6 @@ ecma_builtin_date_prototype_set_full_year (ecma_value_t this_arg, /**< this argu
10061020
ecma_date_time_within_day (t),
10071021
ECMA_DATE_LOCAL);
10081022
}
1009-
ECMA_OP_TO_NUMBER_FINALIZE (y);
10101023
ECMA_FINALIZE (this_time_value);
10111024

10121025
return ret_value;
@@ -1036,27 +1049,31 @@ ecma_builtin_date_prototype_set_utc_full_year (ecma_value_t this_arg, /**< this
10361049
t = ECMA_NUMBER_ZERO;
10371050
}
10381051

1039-
/* 2. Let 'y' be ToNumber('year') where 'year' is args[0]. */
1040-
ECMA_OP_TO_NUMBER_TRY_CATCH (y, args[0], ret_value);
1041-
1042-
/* 3. If 'month' is not specified, then let 'm' be MonthFromTime('t');
1043-
* otherwise, let 'm' be ToNumber('month') where 'month' is args[1]. */
1052+
/* 2. */
1053+
ecma_number_t y = ecma_number_make_nan ();
10441054
ecma_number_t m = ecma_date_month_from_time (t);
10451055
ecma_number_t dt = ecma_date_date_from_time (t);
1046-
if (args_number > 1 && !ecma_is_value_undefined (args[1]))
1056+
if (args_number > 0 && !ecma_is_value_undefined (args[0]))
10471057
{
1048-
ECMA_OP_TO_NUMBER_TRY_CATCH (month, args[1], ret_value);
1049-
m = month;
1058+
ECMA_OP_TO_NUMBER_TRY_CATCH (year, args[0], ret_value);
1059+
y = year;
10501060

1051-
/* 4. If 'date' is not specified, then let 'dt' be DateFromTime('t');
1052-
* otherwise, let 'dt' be ToNumber('date') where 'date' is args[2]. */
1053-
if (args_number > 2 && !ecma_is_value_undefined (args[2]))
1061+
/* 3. */
1062+
if (args_number > 1 && !ecma_is_value_undefined (args[1]))
10541063
{
1055-
ECMA_OP_TO_NUMBER_TRY_CATCH (date, args[2], ret_value);
1056-
dt = date;
1057-
ECMA_OP_TO_NUMBER_FINALIZE (date);
1064+
ECMA_OP_TO_NUMBER_TRY_CATCH (month, args[1], ret_value);
1065+
m = month;
1066+
1067+
/* 4. */
1068+
if (args_number > 2 && !ecma_is_value_undefined (args[2]))
1069+
{
1070+
ECMA_OP_TO_NUMBER_TRY_CATCH (date, args[2], ret_value);
1071+
dt = date;
1072+
ECMA_OP_TO_NUMBER_FINALIZE (date);
1073+
}
1074+
ECMA_OP_TO_NUMBER_FINALIZE (month);
10581075
}
1059-
ECMA_OP_TO_NUMBER_FINALIZE (month);
1076+
ECMA_OP_TO_NUMBER_FINALIZE (year);
10601077
}
10611078

10621079
if (ecma_is_completion_value_empty (ret_value))
@@ -1067,7 +1084,6 @@ ecma_builtin_date_prototype_set_utc_full_year (ecma_value_t this_arg, /**< this
10671084
ecma_date_time_within_day (t),
10681085
ECMA_DATE_UTC);
10691086
}
1070-
ECMA_OP_TO_NUMBER_FINALIZE (y);
10711087
ECMA_FINALIZE (this_time_value);
10721088

10731089
return ret_value;

tests/jerry/date-setters.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,37 @@ assert (d.getUTCDate() == 31);
208208
d.setTime(0);
209209
assert (d.setUTCFullYear(1970) == 0);
210210
assert (d.getUTCFullYear() == 1970);
211+
212+
/* Without argument */
213+
d = new Date();
214+
assert (isNaN (d.setTime()));
215+
assert (isNaN (d.setMilliseconds()));
216+
assert (isNaN (d.setUTCMilliseconds()));
217+
assert (isNaN (d.setSeconds()));
218+
assert (isNaN (d.setUTCSeconds()));
219+
assert (isNaN (d.setMinutes()));
220+
assert (isNaN (d.setUTCMinutes()));
221+
assert (isNaN (d.setHours()));
222+
assert (isNaN (d.setUTCHours()));
223+
assert (isNaN (d.setDate()));
224+
assert (isNaN (d.getUTCDate()));
225+
assert (isNaN (d.setMonth()));
226+
assert (isNaN (d.setUTCMonth()));
227+
assert (isNaN (d.setFullYear()));
228+
assert (isNaN (d.setUTCFullYear()));
229+
230+
assert (isNaN (Date.prototype.setTime()));
231+
assert (isNaN (Date.prototype.setMilliseconds()));
232+
assert (isNaN (Date.prototype.setUTCMilliseconds()));
233+
assert (isNaN (Date.prototype.setSeconds()));
234+
assert (isNaN (Date.prototype.setUTCSeconds()));
235+
assert (isNaN (Date.prototype.setMinutes()));
236+
assert (isNaN (Date.prototype.setUTCMinutes()));
237+
assert (isNaN (Date.prototype.setHours()));
238+
assert (isNaN (Date.prototype.setUTCHours()));
239+
assert (isNaN (Date.prototype.setDate()));
240+
assert (isNaN (Date.prototype.getUTCDate()));
241+
assert (isNaN (Date.prototype.setMonth()));
242+
assert (isNaN (Date.prototype.setUTCMonth()));
243+
assert (isNaN (Date.prototype.setFullYear()));
244+
assert (isNaN (Date.prototype.setUTCFullYear()));

0 commit comments

Comments
 (0)