From 7285a7a8e5dcd2dc24f9599463ebcf9bc4d4d90c Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Fri, 28 Apr 2006 09:01:16 -0400 Subject: [PATCH] libchess: Make sure the move is null terminated after fscanf() in move_text. RAV parsing fixed. Needed to update the g.rav pointer after restoring the last RAV level in rav_text(). Fixed some Valgrind warnings. Fixed some Valgrind warnings in cboard. --- libchess/pgn.c | 8 ++++++-- libchess/pgn.h | 1 + src/cboard.c | 10 ++++++++++ src/rcfile.c | 10 +++++++--- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/libchess/pgn.c b/libchess/pgn.c index 7d8f0b5..c7e1ab5 100644 --- a/libchess/pgn.c +++ b/libchess/pgn.c @@ -832,6 +832,7 @@ static int move_text(GAME *g, FILE *fp) if (fscanf(fp, " %[a-hPRNBQK1-9#+=Ox-]%n", m, &count) != 1) return 1; + m[MAX_SAN_MOVE_LEN] = 0; p = m; if (pgn_parse_move(g, pgn_board, &p)) { @@ -1120,9 +1121,9 @@ static int rav_text(GAME *g, FILE *fp, int which, BOARD o) return 1; } - g->rav = r; + g->rav = pgn_rav_p = r; - if ((g->rav[g->ravlevel].fen = strdup(pgn_game_to_fen((*g), pgn_board))) + if ((g->rav[g->ravlevel].fen = strdup(pgn_game_to_fen(*g, pgn_board))) == NULL) { warn("strdup()"); return 1; @@ -1171,6 +1172,7 @@ static int rav_text(GAME *g, FILE *fp, int which, BOARD o) pgn_board_init_fen(&tg, pgn_board, g->rav[tg.ravlevel].fen); free(g->rav[tg.ravlevel].fen); memcpy(g, &tg, sizeof(GAME)); + g->rav = pgn_rav_p; } /* * The end of a RAV. This makes the read_file() that called this function @@ -2067,6 +2069,8 @@ int pgn_write(FILE *fp, GAME g) break; if (strcmp(g.tag[i]->name, "Date") == 0) { + memset(&tp, 0, sizeof(struct tm)); + if (strptime(g.tag[i]->value, PGN_TIME_FORMAT, &tp) != NULL) { len = strftime(tbuf, sizeof(tbuf), PGN_TIME_FORMAT, &tp) + 1; diff --git a/libchess/pgn.h b/libchess/pgn.h index 4552172..fba32f9 100644 --- a/libchess/pgn.h +++ b/libchess/pgn.h @@ -45,5 +45,6 @@ static int pgn_isfile; static int ravlevel; static long pgn_fsize; static int pgn_rav; +static RAV *pgn_rav_p; #endif diff --git a/src/cboard.c b/src/cboard.c index 4aebba6..1510257 100644 --- a/src/cboard.c +++ b/src/cboard.c @@ -3747,6 +3747,16 @@ void cleanup_all() pgn_free_all(); free(config.engine_cmd); free(config.pattern); + free(config.ccfile); + free(config.nagfile); + free(config.configfile); + + if (config.keys) { + for (i = 0; config.keys[i]; i++) + free(config.keys[i]->str); + + free(config.keys); + } if (config.einit) { for (i = 0; config.einit[i]; i++) diff --git a/src/rcfile.c b/src/rcfile.c index ac8743c..4f0b126 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -217,8 +217,12 @@ void parse_rcfile(const char *filename) if ((n = sscanf(line, "%s %[^\n]", var, val)) != 2) errx(EXIT_FAILURE, "%s(%i): parse error %i", filename, lines,n); - strncpy(val, trim(val), sizeof(val)); - strncpy(var, trim(var), sizeof(var)); + p = strdup(trim(val)); + strncpy(val, p, sizeof(val)); + free(p); + p = strdup(trim(var)); + strncpy(var, p, sizeof(var)); + free(p); if (strcmp(var, "jump_count") == 0) { if (!isinteger(val)) @@ -389,6 +393,6 @@ void parse_rcfile(const char *filename) if (altengine) { free(config.engine_cmd); config.engine_cmd = NULL; - config.engine_cmd = strdup(altengine); + config.engine_cmd = altengine; } } -- 2.11.4.GIT