Bug 13231 – Safe left shift for checkedint

Status
NEW
Severity
enhancement
Priority
P4
Component
druntime
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2014-07-31T20:13:57Z
Last change time
2024-12-07T13:34:03Z
Assigned to
No Owner
Creator
bearophile_hugs
Moved to GitHub: dmd#17280 →

Comments

Comment #0 by bearophile_hugs — 2014-07-31T20:13:57Z
The purpose of checkedint functions/intrinsics is to detect overflows and bugs. When you have a number x of type int/uint/long/ulong where one or more of the most n significant bits is set to 1, and you shift x on left by n, you lose some information, so you have an overflow. So I suggest to add to the checkedint.d module the leftShift functions that perform a checked left shifting. They should also warn against too much large shifts: leftShift(in uint x, in uint n, ref bool overflow) leftShift(in ulong x, in uint n, ref bool overflow) They check that: 1) n is in the appropriate range [0, sizeof(x) * 8]. Otherwise they set overflow to true. 2) None of the n most significant bits of x is 1 before the shifting.
Comment #1 by robert.schadek — 2024-12-07T13:34:03Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17280 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB