Skip to content

Fix is_legal accepting non-canonical castling encodings#1096

Open
grepfuldead wants to merge 1 commit into
codedeliveryservice:mainfrom
grepfuldead:fix/islegal-castling
Open

Fix is_legal accepting non-canonical castling encodings#1096
grepfuldead wants to merge 1 commit into
codedeliveryservice:mainfrom
grepfuldead:fix/islegal-castling

Conversation

@grepfuldead

Copy link
Copy Markdown
Contributor

is_legal derives the castling kind from to.file() == File::G alone and never checks that to is the actual landing square, so with castling rights and a clear path it accepts (king_from, arbitrary to, Castling) for 63 bogus encodings per position. Playing one hits the unreachable in get_castling_rook. Search is unaffected (UCI and the TT path only see canonical encodings), but islegalperft feeds every accepted encoding to make_move and currently panics on any position with castling rights:

position fen r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1
islegalperft 1  ->  panicked: entered unreachable code

Require to == kind.landing_square(), making is_legal an exact mirror of generate_all_moves and restoring the diagnostic (kiwipete islegalperft 1 now completes: 48). Matches the convention in Stockfish, where special-move validity is generated-set membership.

No functional change.

Bench: 2526370

is_legal derives the castling kind from to.file() == File::G alone and
never checks that to is the actual landing square, so with castling
rights and a clear path it accepts (king_from, arbitrary to, Castling)
for 63 bogus encodings per position. Playing one hits the unreachable
in get_castling_rook. Search is unaffected (UCI and the TT path only
see canonical encodings), but islegalperft feeds every accepted
encoding to make_move and currently panics on any position with
castling rights:

    position fen r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1
    islegalperft 1  ->  panicked: entered unreachable code

Require to == kind.landing_square(), making is_legal an exact mirror
of generate_all_moves and restoring the diagnostic (kiwipete
islegalperft 1 now completes: 48). Matches the convention in Stockfish,
where special-move validity is generated-set membership.

No functional change.

Bench: 2526370
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant