Skip to content

Commit d1a0d06

Browse files
committed
Add range check to OP_STR_AT
1 parent 924ca95 commit d1a0d06

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/engine/virtualmachine_p.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,10 +646,16 @@ do_list_get_item : {
646646
FREE_REGS(1);
647647
DISPATCH();
648648

649-
do_str_at:
650-
REPLACE_RET_VALUE(utf8::utf16to8(std::u16string({ READ_REG(0, 2)->toUtf16()[READ_REG(1, 2)->toLong() - 1] })), 2);
649+
do_str_at : {
650+
size_t index = READ_REG(1, 2)->toLong() - 1;
651+
std::u16string str = READ_REG(0, 2)->toUtf16();
652+
if (index < 0 || index >= str.size())
653+
REPLACE_RET_VALUE("", 2);
654+
else
655+
REPLACE_RET_VALUE(utf8::utf16to8(std::u16string({ str[index] })), 2);
651656
FREE_REGS(1);
652657
DISPATCH();
658+
}
653659

654660
do_str_length:
655661
REPLACE_RET_VALUE(static_cast<long>(READ_REG(0, 1)->toUtf16().size()), 1);

0 commit comments

Comments
 (0)