Unexpected path of execution

Adam D Ruppe destructionator at gmail.com
Tue Oct 19 16:38:50 UTC 2021

On Tuesday, 19 October 2021 at 16:20:39 UTC, Charles Hixson wrote:
> given this code fragment:
>             if    (i < (line.length - 3) )
> in c4: i = 0, line.length = 2

line.length is an unsigned value. Arithmetic on an unsigned thing 
is still unsigned.

So UNSIGNED 2 - 3 is not -1, but instead it is size_t.max since 
it rolls over.

Then the comparison also becomes unsigned. So 0 < size_t.max is 
true, meaning it goes in there.

you should be able to fix it if you do

  if(i < (cast(int) line.length) - 3)

to force it to become signed. But this isn't great either. You 
probably want to  change the code to avoid going negative in the 
first place. Maybe test `i + 3 < line.length` instead. Or maybe 
`if(!(i > ... wahtever that is)`. You get the idea im brain 

I personally hate that array.length is unsigned. And I hate that 
the signed/unsigned mixing prefers unsigned instead of just about 
anything else. What a pain in the butt. But that's how it is.

More information about the Digitalmars-d-learn mailing list