Skip to content

Commit abee059

Browse files
committed
Use ToNumber when checking lastIndex during RegExp exec()
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai [email protected]
1 parent 3fbe543 commit abee059

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

jerry-core/ecma/operations/ecma-regexp-object.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,15 +1223,18 @@ ecma_regexp_exec_helper (ecma_object_t *obj_p, /**< RegExp object */
12231223
{
12241224
ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LASTINDEX_UL);
12251225
ecma_property_t *lastindex_prop_p = ecma_op_object_get_property (obj_p, magic_str_p);
1226-
ecma_number_t *lastindex_num_p = ecma_get_number_from_value (lastindex_prop_p->u.named_data_property.value);
1227-
index = ecma_number_to_int32 (*lastindex_num_p);
1226+
1227+
ECMA_OP_TO_NUMBER_TRY_CATCH (lastindex_num, lastindex_prop_p->u.named_data_property.value, ret_value)
1228+
index = ecma_number_to_int32 (lastindex_num);
12281229

12291230
JERRY_ASSERT (iter_p->buf_pos.offset == 0 && !iter_p->buf_pos.is_non_bmp_middle);
12301231
if (!lit_utf8_iterator_is_eos (iter_p)
1231-
&& *lastindex_num_p <= input_str_len)
1232+
&& index <= (int32_t) input_str_len
1233+
&& index > 0)
12321234
{
1233-
lit_utf8_iterator_advance (iter_p, (ecma_length_t) *lastindex_num_p);
1235+
lit_utf8_iterator_advance (iter_p, (ecma_length_t) index);
12341236
}
1237+
ECMA_OP_TO_NUMBER_FINALIZE (lastindex_num);
12351238
ecma_deref_ecma_string (magic_str_p);
12361239
}
12371240

tests/jerry/regexp-assertions.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,27 @@ assert (t == "");
150150

151151
t = new RegExp ("(?!.)").exec("a");
152152
assert (t == "");
153+
154+
t = new RegExp ("abc","g");
155+
t.lastIndex = {toString: function () { return "4"}};
156+
var result = t.exec("abc abc");
157+
assert(result[0] === "abc");
158+
assert(result.index === 6);
159+
160+
t = new RegExp ("abc","g");
161+
t.lastIndex = {valueOf: function () { return "4"}};
162+
var result = t.exec("abc abc");
163+
assert(result[0] === "abc");
164+
assert(result.index === 6);
165+
166+
t = new RegExp ("abc","g");
167+
t.lastIndex = "2"
168+
var result = t.exec("abc abc");
169+
assert(result[0] === "abc");
170+
assert(result.index === 6);
171+
172+
t = new RegExp ("abc","g");
173+
t.lastIndex = -12;
174+
result = t.exec("abc abc");
175+
assert(!result);
176+
assert(t.lastIndex === 0);

0 commit comments

Comments
 (0)