Correctness bug in TDPL
Timon Gehr
timon.gehr at gmx.ch
Fri May 13 11:01:54 PDT 2011
> On 5/13/11 3:25 AM, Timon Gehr wrote:
> > On p368 the CheckedInt struct does not check for overflow in the unary minus
> > operator.
>
> Unary minus never overflows. That being said, there is the oddity that
> -x is x when x == int.min. Even in that case there is no loss of
> information.
>
> Andrei
This behavior is caused by _overflow_ when the error condition that is checked in
++ is overflow:
auto x=CheckedInt(int.min);
x=-x; //passes
x=~x;
x++;//throws
Also, the statement that there is no loss of information is just wrong:
scanf("%d %d %d",&n_,&m_);
auto n=CheckedInt!int(n_),m=CheckedInt!int(m_);
enforce(n>0 && m<0, "provide meaningful input!");
foreach(i;0..n) m=-m;
writeln(n," is"~(m<0?"odd":"even")); //disaster strikes!
Timon
More information about the Digitalmars-d
mailing list