Skip to content

fix undefined left shift of negative int in math.bitShiftLeft#2044

Open
sahvx655-wq wants to merge 1 commit into
google:masterfrom
sahvx655-wq:bitshiftleft-signed-ub
Open

fix undefined left shift of negative int in math.bitShiftLeft#2044
sahvx655-wq wants to merge 1 commit into
google:masterfrom
sahvx655-wq:bitshiftleft-signed-ub

Conversation

@sahvx655-wq
Copy link
Copy Markdown

Noticed bitShiftLeft shifts a signed int64 directly while bitShiftRight goes through an unsigned bit_cast first. For a negative left operand like math.bitShiftLeft(-1, 1) that signed left shift is undefined behaviour, which ubsan flags as left shift of negative value. Shifting in the unsigned domain the way the right shift already does keeps the same bit pattern result without the UB. Added a couple of negative-operand cases to the runtime tests since they weren't covered before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant