Chess.jl icon indicating copy to clipboard operation
Chess.jl copied to clipboard

PIECE_TYPE_NONE out of bounds read

Open mfalt opened this issue 4 years ago • 0 comments

The implementation of empty square is const PIECE_TYPE_NONE = PieceType(7) however, Board has the field bytype::MVector{6,SquareSet}, and the implementation of pieces is

function pieces(b::Board, t::PieceType)::SquareSet
    @inbounds b.bytype[t.val]
end

so the following will result in an out of bounds read (possible crash?)

julia> pieces(startboard(), PIECE_TYPE_NONE)
SquareSet:
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  -

I am not sure how much it would cost to introduce a branch in that statement, but somenthing like the following should be a solution;

function pieces(b::Board, t::PieceType)::SquareSet
    if t.val != 7
        @inbounds b.bytype[t.val]
    else
        return -reduce(union, b.bytype)
    end
end

If the practical cost is too high it might not be worth the fix.

mfalt avatar Oct 24 '21 19:10 mfalt