Bug 4887 – Right-shifting by 32 is allowed and broken
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2010-09-18T03:15:00Z
Last change time
2015-06-09T05:11:54Z
Keywords
accepts-invalid, spec
Assigned to
nobody
Creator
dlang-bugzilla
Comments
Comment #0 by dlang-bugzilla — 2010-09-18T03:15:15Z
const int x = 12345;
static assert(x>>32 == 0); // fails
It looks like x>>32==x when x is a 32-bit or smaller integer.
Same problem at runtime.
I guess that right-shifting by 32 should be forbidden.
Comment #1 by dlang-bugzilla — 2010-09-18T03:16:36Z
BTW, this is a real-world bug that took me several hours to track down. I was writing an application that's 64-bit-agnostic, and was left-shifting a size_t to get the high-order dword.
Comment #2 by dlang-bugzilla — 2010-09-18T03:17:18Z
Oops, right-shifting*
Comment #3 by smjg — 2010-09-18T08:53:09Z
http://www.digitalmars.com/d/1.0/expression.html#ShiftExpression
"It's illegal to shift by more bits than the size of the quantity being shifted:
int c;
c << 33; // error"
However, there's a real problem with that spec: what if the number of bits to shift by isn't known at compile time?
Comment #4 by dlang-bugzilla — 2010-09-18T08:58:22Z
Yeah, I saw that - the problem is with shifting by exactly 32 bits. Runtime behavior of 33 bits or more is a completely different problem...
Comment #5 by clugdbug — 2010-09-18T11:43:17Z
(In reply to comment #3)
> http://www.digitalmars.com/d/1.0/expression.html#ShiftExpression
> "It's illegal to shift by more bits than the size of the quantity being
> shifted:
>
> int c;
> c << 33; // error"
>
> However, there's a real problem with that spec: what if the number of bits to
> shift by isn't known at compile time?
That may be solvable with range propagation. Make it an error if it's not known to be less than 32. It'd be painful to completely implement that rule right now, but perhaps not later on when range propagation becomes more capable. We could at least make it an error if the range of the expression doesn't include ANY values in the legal range, and that would cover this test case.
I suspect that shifts by runtime-determined numbers of bits are relatively rare -- and my experience is that they're quite bug-prone.
Raising priority to major, since this is a nasty trap.
Comment #6 by smjg — 2010-09-18T12:12:33Z
(In reply to comment #5)
> (In reply to comment #3)
> > http://www.digitalmars.com/d/1.0/expression.html#ShiftExpression
> > "It's illegal to shift by more bits than the size of the quantity being
> > shifted:
> >
> > int c;
> > c << 33; // error"
> >
> > However, there's a real problem with that spec: what if the number of bits to
> > shift by isn't known at compile time?
>
> That may be solvable with range propagation. Make it an error if it's not known
> to be less than 32. It'd be painful to completely implement that rule right
> now, but perhaps not later on when range propagation becomes more capable. We
> could at least make it an error if the range of the expression doesn't include
> ANY values in the legal range, and that would cover this test case.
> I suspect that shifts by runtime-determined numbers of bits are relatively rare
> -- and my experience is that they're quite bug-prone.
>
> Raising priority to major, since this is a nasty trap.
But range propagation isn't going to be implemented in D1, or is it?
Trying it in 1.064 (Windows):
* (u)int << 32 or (u)int >> 32 is accepted by the compiler, but is a nop
* (u)int << 33 or (u)int >> 33 is rejected by the compiler, but if the 33 is passed through a variable then it's equivalent to shifting by 1
* Shifting a (u)long by 32 or 33 seems to work, but the bug affects shifting one by 64 or 65.
What is the "Other" platform for which this bug is filed? (I'm guessing the behaviour to be processor-dependent. Mine, for the record, is a 2.4GHz Intel Core Duo.)
Comment #7 by dlang-bugzilla — 2010-09-18T12:16:26Z
(In reply to comment #6)
> What is the "Other" platform for which this bug is filed? (I'm guessing the
> behaviour to be processor-dependent. Mine, for the record, is a 2.4GHz Intel
> Core Duo.)
Sorry, I forgot to fill out that field.
Pretty sure things like this have to be deterministic across x86 implementations. Of course the behavior in 64-bit programs may differ.
Comment #8 by ah08010-d — 2010-10-03T18:40:49Z
I encountered a similar problem. I was taking code I had written with ulongs and trying to template-ize the code. For me, the code below prints "plain error?".
======
import std.stdio;
void main() {
uint foo = 0;
plain_sub( foo );
}
int plain_sub( const uint value ) {
if( uint t32 = value >> 32 ) {
writeln( "plain error?" );
}
return 0;
}
=====
C99 says that shifts >= the width of the victim are undefined behavior. The D2 manual says shifting /more/ than the width of the victim is illegal.
Apparently, shifting equal to the width is legal-but-surprising. I'd like it to be either illegal, with a warning, or legal-but-not-surprising.
Comment #9 by github-bugzilla — 2012-01-23T23:08:33Z