Skip to content

Commit a6ab5e9

Browse files
authored
Fix runtime error: left shift (#4912)
This patch fixes #4703 This patch fixes #4702 JerryScript-DCO-1.0-Signed-off-by: Daniel Batiz [email protected]
1 parent a63e1d2 commit a6ab5e9

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

jerry-core/ecma/base/ecma-helpers-number.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,15 @@ ecma_integer_multiply (ecma_integer_value_t left_integer, /**< left operand */
414414
if (JERRY_UNLIKELY ((left_integer & (left_integer - 1)) == 0))
415415
{
416416
/* Right shift right_integer with log2 (left_integer) */
417-
return ecma_make_integer_value (right_integer << (__builtin_ctz ((unsigned int) left_integer)));
417+
return ecma_make_integer_value (
418+
(int32_t) ((uint32_t) right_integer << (__builtin_ctz ((unsigned int) left_integer))));
418419
}
419420

420421
if (JERRY_UNLIKELY ((right_integer & (right_integer - 1)) == 0))
421422
{
422423
/* Right shift left_integer with log2 (right_integer) */
423-
return ecma_make_integer_value (left_integer << (__builtin_ctz ((unsigned int) right_integer)));
424+
return ecma_make_integer_value (
425+
(int32_t) ((uint32_t) left_integer << (__builtin_ctz ((unsigned int) right_integer))));
424426
}
425427
#endif /* defined (__GNUC__) || defined (__clang__) */
426428

jerry-core/vm/vm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3765,7 +3765,8 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
37653765
{
37663766
ecma_integer_value_t left_integer = ecma_get_integer_from_value (left_value);
37673767
ecma_integer_value_t right_integer = ecma_get_integer_from_value (right_value);
3768-
*stack_top_p++ = ecma_make_int32_value ((int32_t) (left_integer << (right_integer & 0x1f)));
3768+
3769+
*stack_top_p++ = ecma_make_int32_value ((int32_t) ((uint32_t) left_integer << (right_integer & 0x1f)));
37693770
continue;
37703771
}
37713772

tests/jerry/shift.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414

1515
assert((9 << 2) === 36);
1616
assert((14 << 2) === 56);
17+
assert((0 << 0) === 0);
18+
assert((0 << 1) === 0);
19+
assert((0 << -1) === 0);
20+
assert((1 << 0) === 1);
21+
assert((1 << 1) === 2);
22+
assert((-1 << 0) === -1);
23+
assert((-1 << 1) === -2);
24+
assert((1024 << 21) === -2147483648);
25+
assert((10 << -1) === 0);
26+
assert((33554431 << 22) === -4194304);
27+
assert((-1024 << 31) === 0);
28+
assert((1024 << 53) === -2147483648);
1729

1830
assert((9 >> 2) === 2);
1931
assert((-14 >> 2) === -4);

0 commit comments

Comments
 (0)