From 1afda36d85e01a2f774d8743dd0aff7c228b0a84 Mon Sep 17 00:00:00 2001 From: Peter Osterlund Date: Fri, 3 Jan 2020 10:32:49 +0100 Subject: [PATCH] Don't try to fixup invalid e.p. square if the position is invalid En passant fixup requires generating a list of legal moves, which can fail with an array index out of bounds exception if there is a pawn on the first/last rank. --- .../org/petero/droidfish/activities/EditBoard.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditBoard.java b/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditBoard.java index a7f0e0e..f1df5d5 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditBoard.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditBoard.java @@ -442,7 +442,8 @@ public class EditBoard extends Activity { private void setPosFields() { setEPFile(getEPFile()); // To handle sideToMove change - TextIO.fixupEPSquare(cb.pos); + if (isValid()) + TextIO.fixupEPSquare(cb.pos); TextIO.removeBogusCastleFlags(cb.pos); } @@ -478,6 +479,16 @@ public class EditBoard extends Activity { return false; } + /** Return true if the position is valid. */ + private boolean isValid() { + try { + TextIO.readFEN(TextIO.toFEN(cb.pos)); + return true; + } catch (ChessParseError e) { + return false; + } + } + private String getParseErrString(ChessParseError e) { if (e.resourceId == -1) return e.getMessage();