Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion extensions/math_ext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ Value BitShiftLeftInt(int64_t lhs, int64_t rhs) {
if (rhs > 63) {
return IntValue(0);
}
return IntValue(lhs << static_cast<int>(rhs));
// Shift in the unsigned domain to avoid undefined behaviour when lhs is
// negative or the shift moves bits into the sign bit, matching the bit
// pattern semantics already used by bitShiftRight.
return IntValue(absl::bit_cast<int64_t>(absl::bit_cast<uint64_t>(lhs)
<< static_cast<int>(rhs)));
}

Value BitShiftLeftUint(uint64_t lhs, int64_t rhs) {
Expand Down
2 changes: 2 additions & 0 deletions extensions/math_ext_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,8 @@ INSTANTIATE_TEST_SUITE_P(
{"math.bitNot(2) == -3"},
{"math.bitAnd(math.bitNot(0x3u), 0xFFu) == 0xFCu"},
{"math.bitShiftLeft(1, 1) == 2"},
{"math.bitShiftLeft(-1, 1) == -2"},
{"math.bitShiftLeft(-4, 2) == -16"},
{"math.bitShiftLeft(1u, 1) == 2u"},
{"math.bitShiftRight(4, 1) == 2"},
{"math.bitShiftRight(4u, 1) == 2u"}}));
Expand Down