Thread overview
[Issue 24411] [CODEGEN] bad shl codegen
Feb 27
Manu
Feb 27
Dennis
February 27
https://issues.dlang.org/show_bug.cgi?id=24411

--- Comment #1 from anonymous4 <dfj1esp02@sneakemail.com> ---
Yes, that's how shift normally works: https://dlang.org/spec/expression.html#shift_expressions

I believe, the goto solution here is checked int.

--
February 27
https://issues.dlang.org/show_bug.cgi?id=24411

--- Comment #2 from Manu <turkeyman@gmail.com> ---
Okay, my bad. It's in the spec!

Surprising; dlang uses prides itself on not having surprise invisible undefined
behaviour littered around your code.
This seems like a safety concern; it's conceivable an exploit could be written
taking advantage of this undefined behaviour.

--
February 27
https://issues.dlang.org/show_bug.cgi?id=24411

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |dkorpel@live.nl
         Resolution|---                         |INVALID

--- Comment #3 from Dennis <dkorpel@live.nl> ---
The key here is that it's specified as "implementation defined behavior", not "undefined behavior". It could give a bogus integer and lead to logic bugs, but it can't result in memory corruption in `@safe` code. D's 'safety' is specifically targeting memory safety, not logic bugs in general (e.g. unintentional integer overflow). It's still a systems programming language with similar performance to C. Introducing bounds checks to shift expressions is a big performance hit, especially considering shifts are usually found in bit-twiddling performance sensitive code.

--
February 28
https://issues.dlang.org/show_bug.cgi?id=24411

--- Comment #4 from anonymous4 <dfj1esp02@sneakemail.com> ---
Shifts are often hardcoded. If you shift by untrusted amount, then you probably have bit arrays, and if you use bit arrays with untrusted indexes, then you need bound checking, not clear what you try to do, try https://dlang.org/phobos/std_bitmanip.html#BitArray

AFAIK most processors simply mask the shift amount. If some processor traps on overflow here, it would be safe, but probably not very useful for you.

--