fix wrong error message when move indices are invalid

Fri, 24 Jul 2026 12:42:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 24 Jul 2026 12:42:57 +0200
changeset 153
bad2d6d4b861
parent 152
231e5f07a657
child 154
5336e985bd69

fix wrong error message when move indices are invalid

was INVALID_POSITION "no piece cannot be moved this way"
but should be INVALID_MOVE_SYNTAX

src/chess/rules.c file | annotate | diff | comparison | revisions
src/chess/rules.h file | annotate | diff | comparison | revisions
--- a/src/chess/rules.c	Thu Jul 23 11:37:35 2026 +0200
+++ b/src/chess/rules.c	Fri Jul 24 12:42:57 2026 +0200
@@ -337,8 +337,9 @@
 
 static int validate_move_rules(GameState *gamestate, Move *move) {
     /* validate indices (don't trust opponent) */
-    if (!chkidx(move)) {
-        return INVALID_POSITION;
+    if (!isidx(move->fromrow) || !isidx(move->fromfile) ||
+        !isidx(move->torow) || !isidx(move->tofile)) {
+        return INVALID_MOVE_SYNTAX;
     }
     
     /* must move */
@@ -844,8 +845,13 @@
 
     move->piece |= color;
 
-    if (!chkidx_to(move)) {
-        return INVALID_POSITION;
+    /* up to this point
+     * destination indices must be specified and valid
+     * source indices must either be valid or unspecified
+     */
+    if (!isidxr(move->fromrow) || !isidxr(move->fromfile) ||
+        !isidx(move->torow) || !isidx(move->tofile)) {
+        return INVALID_MOVE_SYNTAX;
     }
 
     return VALID_MOVE_SYNTAX;
@@ -858,8 +864,6 @@
         if (move->fromfile == POS_UNSPECIFIED
             || move->fromrow == POS_UNSPECIFIED) {
             return getlocation(gamestate, move);
-        } else if (!chkidx_from(move)) {
-            return INVALID_POSITION;
         }
     }
     return result;
--- a/src/chess/rules.h	Thu Jul 23 11:37:35 2026 +0200
+++ b/src/chess/rules.h	Fri Jul 24 12:42:57 2026 +0200
@@ -145,7 +145,10 @@
 #define mdst(b,m) b[(m)->torow][(m)->tofile]
 #define msrc(b,m) b[(m)->fromrow][(m)->fromfile]
 
+/** Checks if the index is specified and valid. */
 #define isidx(idx) ((uint8_t)(idx) < 8)
+/** Checks if the index is unspecified or valid. */
+#define isidxr(idx) ((idx) == POS_UNSPECIFIED || (uint8_t)(idx) < 8)
 
 #define isfile(file) (file >= 'a' && file <= 'h')
 #define isrow(row) (row >= '1' && row <= '8')
@@ -156,10 +159,6 @@
 #define rowchr(row) (row+'1')
 #define filechr(file) (file+'a')
 
-#define chkidx_from(move) (isidx((move)->fromfile) && isidx((move)->fromrow))
-#define chkidx_to(move) (isidx((move)->tofile) && isidx((move)->torow))
-#define chkidx(move) (chkidx_from(move) && chkidx_to(move))
-
 /* secure versions - use, if index is not checked with isidx() */
 #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED)
 #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED)

mercurial