Bug 12088 – Bugs caused by using logical 'and' instead of bitwise 'and'
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-02-06T02:33:12Z
Last change time
2023-06-29T15:14:44Z
Assigned to
No Owner
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2014-02-06T02:33:12Z
This is not an enhancement request, it shows a language small design problem.
D gives no warnings nor errors on this code:
void main() {
uint x;
ubyte y = (x >> 16) && 0xFF;
}
Here to extract a ubyte the programmer has used a logical 'and' instead of a bitwise 'and' by mistake. And it's not an uncommon mistake.
Ideas to face this kind of bugs are welcome.
Comment #1 by razvan.nitu1305 — 2023-06-29T09:34:54Z
Unfortunately, there is nothing the compiler can do in this situation, since that is valid code. This could be a check that a tool using the compiler as a library could implement.
Comment #2 by nick — 2023-06-29T15:14:44Z
`some_integer && int_literal` could be made an error.