Fix variant parsing for invalid board size
authorFabian Fichter <ianfab@users.noreply.github.com>
Sat, 10 Dec 2022 17:21:40 +0000 (18:21 +0100)
committerFabian Fichter <ianfab@users.noreply.github.com>
Sat, 10 Dec 2022 17:21:40 +0000 (18:21 +0100)
src/parser.cpp
src/variant.h

index eda6346..196cfca 100644 (file)
@@ -378,8 +378,6 @@ Variant* VariantParser<DoCheck>::parse(Variant* v) {
     parse_attribute("materialCounting", v->materialCounting);
     parse_attribute("countingRule", v->countingRule);
 
-    v->conclude(); // In preparation for the consistency checks below, in case conclude() hasn't been called yet.
-
     // Report invalid options
     if (DoCheck)
     {
@@ -399,6 +397,8 @@ Variant* VariantParser<DoCheck>::parse(Variant* v) {
                     std::cerr << piece_name(pt) << " - Ambiguous piece character: " << v->pieceToChar[make_piece(c, pt)] << std::endl;
         }
 
+        v->conclude(); // In preparation for the consistency checks below
+
         // startFen
         if (FEN::validate_fen(v->startFen, v, v->chess960) != FEN::FEN_OK)
             std::cerr << "startFen - Invalid starting position: " << v->startFen << std::endl;
index a54e541..d76e908 100644 (file)
@@ -240,8 +240,10 @@ struct Variant {
       // Map king squares to enumeration of actually available squares.
       // E.g., for xiangqi map from 0-89 to 0-8.
       // Variants might be initialized before bitboards, so do not rely on precomputed bitboards (like SquareBB).
+      // Furthermore conclude() might be called on invalid configuration during validation,
+      // therefore skip proper initialization in case of invalid board size.
       int nnueKingSquare = 0;
-      if (nnueKing)
+      if (nnueKing && nnueSquares <= SQUARE_NB)
           for (Square s = SQ_A1; s < nnueSquares; ++s)
           {
               Square bitboardSquare = Square(s + s / (maxFile + 1) * (FILE_MAX - maxFile));