Potential bug with array.Length comparison

BlitzMax Forums/Brucey's Modules/Potential bug with array.Length comparison

Bobysait(Posted 2016) [#1]
Using blitzmax-NG, I 've got a weird bug while checking int value is lower than an array's length
(I spent a lot of time debugging a pretty complex code that didn't work and I couldn't point on the problem until I found that it did never enter the array's loop in the code and it didn't work because of "-1 < array.Lenght" returns "False")

So here is a simplified code that gives the same result :
Create an array of anything, and just compare the Length to an Integer value

SuperStrict

Local array:Float[] = New Float[12];

Local a:Int = -1
Local b:Int = 1
Local c:Long = 1

Print "a="+a
Print "b="+b
Print "c="+c
Print "array.Length="+array.Length
Print "a<b: "+(a<b)
Print "a<c: "+(a<c)
Print "a<array.Length: "+(a<array.Length)
End


This gives me :

a=-1
b=1
c=1
array.Length=12
a<b: 1
a<c: 1
a<array.Length: 0


a<array.Length should be "1"

For now, I can fix it by modifying the syntax and using a temporary variable
Local d:Int = array.Length
Print a<d


But there still is a real bug here.

it seems it can also be fixed by comparing length to a Long instead of an Int (It means ".Length" returns a long ? and it would also mean we can't compare int to long ? it doesn't really make sense as I can compare Int to Long -> "a<c" works while a is an Int and c is a Long ... so anyway, there still is something wrong there)


Bobysait(Posted 2016) [#2]
If this helps, here are the results of casting "a" and "Array.Length" to Int/Long
(some works, some does not)

SuperStrict

Local array:Float[] = New Float[12];

Local a:Int = -1

Print (a<array.Length)
Print (a<Int(array.Length))
Print (a<Long(array.Length))
Print (Int(a)<array.Length)
Print (Int(a)<Int(array.Length))
Print (Int(a)<Long(array.Length))
Print (Long(a)<array.Length)
Print (Long(a)<Int(array.Length))
Print (Long(a)<Long(array.Length))
End


result :
0
0
1
0
0
1
1
1
1



Derron(Posted 2016) [#3]
The corresponding C-Code of the last print is:

brl_standardio_Print(bbStringConcat(&_s6,bbStringFromInt(bbt_a<(bbt_array->scales[0]))));



Maybe not directly an issue with NG, but with what the C-code is doing.


It also gets interesting if you "switch" comparison sign:

Print "a<array.Length: "+(a<array.Length)
Print "a>array.Length: "+(a>array.Length)

'prints
a<array.Length: 0
a>array.Length: 1


with c-code being:
[...]
BBARRAY bbt_array=bbArrayNew1D("f", 12);
BBINT bbt_a=-1;
BBINT bbt_b=1;		

brl_standardio_Print(bbStringConcat(&_s6,bbStringFromInt(bbt_a<(bbt_array->scales[0]))));
brl_standardio_Print(bbStringConcat(&_s7,bbStringFromInt(bbt_a>(bbt_array->scales[0]))));


bye
Ron


Derron(Posted 2016) [#4]
Update your bcc-ng..
Brucey pushed a fix. Seems array.length returned an unsigned int...which flawfully compared to the given signed integer.



Bye
Ron


Bobysait(Posted 2016) [#5]
Oh very nice ! a fix in the night :)