Sat, 18 Jul 2026 21:18:38 +0200
make eval_move() and check_move() not change their input string
| src/chess/rules.c | file | annotate | diff | comparison | revisions | |
| src/chess/rules.h | file | annotate | diff | comparison | revisions |
--- a/src/chess/rules.c Sat Jul 18 21:18:07 2026 +0200 +++ b/src/chess/rules.c Sat Jul 18 21:18:38 2026 +0200 @@ -716,15 +716,17 @@ } } -static int eval_move1(char *mstr, Move *move, uint8_t color) { +static int eval_move1(const char *pstr, Move *move, uint8_t color) { memset(move, 0, sizeof(Move)); move->fromfile = POS_UNSPECIFIED; move->fromrow = POS_UNSPECIFIED; - size_t len = strlen(mstr); + size_t len = strlen(pstr); if (len < 1 || len > 6) { return INVALID_MOVE_SYNTAX; } + char mstr[8]; + strcpy(mstr, pstr); /* evaluate check/checkmate flags */ if (mstr[len-1] == '+' || mstr[len-1] == '#') { @@ -844,7 +846,8 @@ return VALID_MOVE_SYNTAX; } -int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color) { +int eval_move(GameState *gamestate, const char *mstr, + Move *move, uint8_t color) { int result = eval_move1(mstr, move, color); if (result == VALID_MOVE_SYNTAX) { if (move->fromfile == POS_UNSPECIFIED @@ -857,7 +860,7 @@ return result; } -int check_move(char *mstr, uint8_t color) { +int check_move(const char *mstr, uint8_t color) { Move move; return eval_move1(mstr, &move, color); }
--- a/src/chess/rules.h Sat Jul 18 21:18:07 2026 +0200 +++ b/src/chess/rules.h Sat Jul 18 21:18:38 2026 +0200 @@ -334,7 +334,8 @@ * @param color the color of the player to evaluate the move for * @return status code (see macros in this file for the list of codes) */ -int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color); +int eval_move(GameState *gamestate, const char *mstr, + Move *move, uint8_t color); /** * Syntactically checks a move without verifying that a piece exists that is @@ -344,7 +345,7 @@ * @param color the color of the player to evaluate the move for * @return status code (see macros in this file for the list of codes) */ -int check_move(char *mstr, uint8_t color); +int check_move(const char *mstr, uint8_t color); /** * Validates move by applying chess rules.