ooc-lang / rock

:ocean: self-hosted ooc compiler that generates c99
http://ooc-lang.org/
MIT License
404 stars 40 forks source link

ArrayList bound checks twice when getting an element #941

Closed alexnask closed 8 years ago

alexnask commented 8 years ago
get: inline func(index: SSizeT) -> T {
        if(index < 0) index = _size + index
        if(index < 0 || index >= _size) OutOfBoundsException new(This, index, _size) throw()
        checkIndex(index)
        return data[index]
    }

    checkIndex: inline func (index: SSizeT) {
        if (index >= _size) {
            OutOfBoundsException new(This, index, _size) throw()
        }
    }
alexnask commented 8 years ago

Removing the checkIndex call seems like the right thing to do, as the negative index needs to be re-checked.

fasterthanlime commented 8 years ago

@shamanas what if index < -_size ?

edit: I can't read. Yeah I guess someone forgot to remove the checkIndex call

alexnask commented 8 years ago

Sure, removing the checkIndex call and leaving the second if is my proposal.

Then, if index < -_size, it is still caught, as well as the index >= _size check that is done twice.

fasterthanlime commented 8 years ago

sounds good