Bug? Array boundary error when returning indexed element from array

miker2069

Active Member
Licensed User
Longtime User
I encountered this (what I think is a) bug. If I try and return second element (array index 1) from the int array returned from BytesConverter.IntsFromBytes, I get an Array out of bounds error at the point of the return. For example the following code generates the array boundary error:

B4X:
Public Sub GetStoredDataLength As Int
    Dim header() As Byte = eeprom.ReadBytes(0, 4)  'stored 2 ints, first int is the magic number second is stored data length

    If Main.bc.IntsFromBytes(header)(0) = MAGIC_EEPROM Then
        Log("Magic EEPROM Matched - we've been here before")
        Log("Stored length: " , Main.bc.IntsFromBytes(header)(1)) '<-prints correct value
        Dim len As Int =  Main.bc.IntsFromBytes(header)(1) '<-correct value is stored

        Return Main.bc.IntsFromBytes(header)(1) '< error generated
    End If
    Return 0
End Sub

The following adjusted code works fine (basically using the value stored in len)

B4X:
Public Sub GetStoredDataLength As Int
    Dim header() As Byte = eeprom.ReadBytes(0, 4) 'stored 2 ints, first int is the magic number second is stored data length

    If Main.bc.IntsFromBytes(header)(0) = MAGIC_EEPROM Then
        Log("Magic EEPROM Matched - we've been here before")
        Log("Stored length: " , Main.bc.IntsFromBytes(header)(1))
        Dim len As Int =  Main.bc.IntsFromBytes(header)(1)
        Return len ' <-correct value returned
    End If
    Return 0
End Sub

Not a biggy (to me) but thought I'd report it. The code in first example compiles fine just throws an error when returning the second element of the array. May not have anything to do with ByteConverter and might occur when returning the index element of any array.

Work around is set the return value to some temp variable and return that.
 

miker2069

Active Member
Licensed User
Longtime User
Thank you for the fix! So in the next release of B4R or is this ByteConverter update?
 
Top