Bug? Array boundary error when returning indexed element from array

Discussion in 'B4R Bugs & Wishlist' started by miker2069, Sep 16, 2017.

  1. miker2069

    miker2069 Active Member Licensed 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:

    Code:
    Public Sub GetStoredDataLength As Int
        
    Dim header() As Byte = eeprom.ReadBytes(04)  '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)

    Code:
    Public Sub GetStoredDataLength As Int
        
    Dim header() As Byte = eeprom.ReadBytes(04'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.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    That was a tricky one. It is fixed for the next update.
     
    miker2069 likes this.
  3. miker2069

    miker2069 Active Member Licensed User

    Thank you for the fix! So in the next release of B4R or is this ByteConverter update?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Next release of B4R.
     
    miker2069 likes this.
Loading...