From 15c53105123b01d133205bf35ac038b5e0681292 Mon Sep 17 00:00:00 2001 From: Alexander Kernozhitsky Date: Fri, 16 Aug 2024 03:00:15 +0200 Subject: [PATCH] Fix panic when converting invalid pawn moves from SAN --- chess/src/moves/san.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/chess/src/moves/san.rs b/chess/src/moves/san.rs index df6d5fa..2bbaac8 100644 --- a/chess/src/moves/san.rs +++ b/chess/src/moves/san.rs @@ -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())); @@ -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; @@ -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 [