# HG changeset patch # User Mike Becker # Date 1784889777 -7200 # Node ID bad2d6d4b8619ce399c31f3f2331d880f8499b2f # Parent 231e5f07a657c8fe1dab94dd6eabfe8c0e02b95b 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 diff -r 231e5f07a657 -r bad2d6d4b861 src/chess/rules.c --- 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; diff -r 231e5f07a657 -r bad2d6d4b861 src/chess/rules.h --- 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)