Skip to content

Commit

Permalink
Fix panic when converting invalid pawn moves from SAN
Browse files Browse the repository at this point in the history
  • Loading branch information
alex65536 committed Aug 16, 2024
1 parent 35f3e69 commit 15c5310
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions chess/src/moves/san.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl Data {
Ok(mv)
}
Self::PawnMove { dst, promote } => {
if dst.rank() == geometry::promote_src_rank(b.side().inv()) {
if dst.rank() == geometry::promote_dst_rank(b.side().inv()) {
return Err(IntoMoveError::Create(CreateError::NotWellFormed));
}
let mut src = dst.add(-geometry::pawn_forward_delta(b.side()));
Expand All @@ -397,7 +397,7 @@ impl Data {
Ok(mv)
}
Self::PawnCapture { src, dst, promote } => {
if dst.rank() == geometry::promote_src_rank(b.side().inv()) {
if dst.rank() == geometry::promote_dst_rank(b.side().inv()) {
return Err(IntoMoveError::Create(CreateError::NotWellFormed));
}
let mut kind = MoveKind::Simple;
Expand Down Expand Up @@ -937,6 +937,23 @@ mod tests {
);
}

#[test]
fn test_pawns_bad() {
let b = Board::from_str("k5K1/8/p4q2/1P4n1/8/2P5/5q2/8 b - - 0 1").unwrap();
assert_eq!(
base::Move::from_san("bxa8", &b),
Err(ParseError::Convert(IntoMoveError::Create(
CreateError::NotWellFormed
)))
);
assert_eq!(
base::Move::from_san("c8", &b),
Err(ParseError::Convert(IntoMoveError::Create(
CreateError::NotWellFormed
)))
);
}

#[test]
fn test_pawns() {
for (fen_str, uci_str, mv_str, real_mv_str) in [
Expand Down

0 comments on commit 15c5310

Please sign in to comment.