diff options
Diffstat (limited to 'FICS/obsproc.c')
-rw-r--r-- | FICS/obsproc.c | 144 |
1 files changed, 88 insertions, 56 deletions
diff --git a/FICS/obsproc.c b/FICS/obsproc.c index af8bf2a..2353c60 100644 --- a/FICS/obsproc.c +++ b/FICS/obsproc.c @@ -29,6 +29,10 @@ Markus Uhlin 24/07/07 Fixed unhandled return values of fscanf(). Markus Uhlin 24/12/02 Improved old_mail_moves() + Markus Uhlin 25/01/18 Fixed -Wshadow + Markus Uhlin 25/03/15 Fixed possible buffer overflow + in FindHistory2(). + Markus Uhlin 25/04/06 Fixed Clang Tidy warnings. */ #include "stdinclude.h" @@ -87,19 +91,22 @@ GameNumFromParam(int p, int *p1, parameter *param) PRIVATE int gamesortfunc(const void *i, const void *j) { + const int x = *(int *)i; + const int y = *(int *)j; + /* * examine mode games moved to top of "games" output */ - return (GetRating(&parray[garray[*(int *)i].white], - garray[*(int *)i].type) + - GetRating(&parray[garray[*(int *)i].black], - garray[*(int *)i].type) - - (garray[*(int *)i].status == GAME_EXAMINE ? 10000 : 0) - - GetRating(&parray[garray[*(int *)j].white], - garray[*(int *)j].type) - - GetRating(&parray[garray[*(int *)j].black], - garray[*(int *)j].type) + - (garray[*(int *)j].status == GAME_EXAMINE ? 10000 : 0)); + return (GetRating(&parray[garray[x].white], + garray[x].type) + + GetRating(&parray[garray[x].black], + garray[x].type) - + (garray[x].status == GAME_EXAMINE ? 10000 : 0) - + GetRating(&parray[garray[y].white], + garray[y].type) - + GetRating(&parray[garray[y].black], + garray[y].type) + + (garray[y].status == GAME_EXAMINE ? 10000 : 0)); } PUBLIC int @@ -146,6 +153,9 @@ com_games(int p, param_list param) wp = garray[i].white; bp = garray[i].black; + UNUSED_VAR(wp); + UNUSED_VAR(bp); + if ((!selected) && s && strncasecmp(s, garray[i].white_name, slen) && @@ -374,9 +384,8 @@ com_allobservers(int p, param_list param) start = 0; end = g_num; } else if ((obgame >= g_num) || - ((obgame < g_num) && - ((garray[obgame].status != GAME_ACTIVE) && - (garray[obgame].status != GAME_EXAMINE)))) { + (garray[obgame].status != GAME_ACTIVE && + garray[obgame].status != GAME_EXAMINE)) { pprintf(p, "There is no such game.\n"); return COM_OK; } else { @@ -954,7 +963,7 @@ ExamineAdjourned(int p, int p1, int p2) } PRIVATE char * -FindHistory(int p, int p1, int game) +FindHistory(int p, int p1, int p_game) { FILE *fpHist; int index = 0; @@ -974,12 +983,17 @@ FindHistory(int p, int p1, int game) ret = fscanf(fpHist, "%d %*c %*d %*c %*d %*s %*s %*d %*d %*d " "%*d %*s %*s %ld", &index, &when); - if (ret != 2) - warn("%s: %s: corrupt", __func__, &fileName[0]); - } while (!feof(fpHist) && index != game); + if (ret != 2) { + warnx("%s: %s: corrupt", __func__, fileName); + fclose(fpHist); + return NULL; + } + } while (!feof(fpHist) && + !ferror(fpHist) && + index != p_game); - if (feof(fpHist)) { - pprintf(p, "There is no history game %d for %s.\n", game, + if (feof(fpHist) || ferror(fpHist)) { + pprintf(p, "There is no history game %d for %s.\n", p_game, parray[p1].name); fclose(fpHist); return NULL; @@ -993,9 +1007,10 @@ FindHistory(int p, int p1, int game) } PRIVATE char * -FindHistory2(int p, int p1, int game, char *End) +FindHistory2(int p, int p1, int p_game, char *End, const size_t End_size) { FILE *fpHist; + char fmt[80] = { '\0' }; int index = 0; long int when = 0; static char fileName[MAX_FILENAME_SIZE]; @@ -1008,17 +1023,21 @@ FindHistory2(int p, int p1, int game, char *End) return NULL; } - do { - int ret; + msnprintf(fmt, sizeof fmt, "%%d %%*c %%*d %%*c %%*d %%*s %%*s %%*d " + "%%*d %%*d %%*d %%*s %%%zus %%ld\n", (End_size - 1)); - ret = fscanf(fpHist, "%d %*c %*d %*c %*d %*s %*s %*d %*d %*d " - "%*d %*s %s %ld", &index, End, &when); - if (ret != 3) - warn("%s: %s: corrupt", __func__, &fileName[0]); - } while (!feof(fpHist) && index != game); + do { + if (fscanf(fpHist, fmt, &index, End, &when) != 3) { + warnx("%s: %s: corrupt", __func__, fileName); + fclose(fpHist); + return NULL; + } + } while (!feof(fpHist) && + !ferror(fpHist) && + index != p_game); - if (feof(fpHist)) { - pprintf(p, "There is no history game %d for %s.\n", game, + if (feof(fpHist) || ferror(fpHist)) { + pprintf(p, "There is no history game %d for %s.\n", p_game, parray[p1].name); fclose(fpHist); return NULL; @@ -1032,15 +1051,15 @@ FindHistory2(int p, int p1, int game, char *End) } PRIVATE void -ExamineHistory(int p, int p1, int game) +ExamineHistory(int p, int p1, int p_game) { FILE *fpGame; char *fileName; - if ((fileName = FindHistory(p, p1, game)) != NULL) { + if ((fileName = FindHistory(p, p1, p_game)) != NULL) { if ((fpGame = fopen(fileName, "r")) == NULL) { pprintf(p, "History game %d not available for %s.\n", - game, parray[p1].name); + p_game, parray[p1].name); } else { ExamineStored(fpGame, p, fileName); fclose(fpGame); @@ -1751,21 +1770,13 @@ com_journal(int p, param_list param) PRIVATE void jsave_journalentry(int p, char save_spot, int p1, char from_spot, char *to_file) { - FILE *Game; - char *name_from = parray[p1].login; - char *name_to = parray[p].login; - char BlackName[MAX_LOGIN_NAME + 1]; - char WhiteName[MAX_LOGIN_NAME + 1]; - char command[MAX_FILENAME_SIZE * 2 + 3]; - char eco[100]; - char ending[100]; - char fname[MAX_FILENAME_SIZE]; - char fname2[MAX_FILENAME_SIZE]; - char result[100]; - char type[100]; - int BlackRating; - int WhiteRating; - int i, t; + FILE *Game; + char *name_from = parray[p1].login; + char *name_to = parray[p].login; + char command[MAX_FILENAME_SIZE * 2 + 3]; + char fname[MAX_FILENAME_SIZE]; + char fname2[MAX_FILENAME_SIZE]; + struct JGI_context ctx; msnprintf(fname, sizeof fname, "%s/%c/%s.%c", journal_dir, name_from[0], name_from, from_spot); @@ -1795,14 +1806,34 @@ jsave_journalentry(int p, char save_spot, int p1, char from_spot, char *to_file) msnprintf(fname, sizeof fname, "%s/player_data/%c/%s.%s", stats_dir, name_to[0], name_to, STATS_JOURNAL); - if (!journal_get_info(p, from_spot, WhiteName, &WhiteRating, - BlackName, &BlackRating, type, &t, &i, eco, ending, result, fname)) + /* + * Init context + */ + ctx.p = p; + ctx.from_spot = from_spot; + ctx.WhiteRating = 0; + ctx.BlackRating = 0; + ctx.t = 0; + ctx.i = 0; + memset(ctx.WhiteName, 0, sizeof(ctx.WhiteName)); + memset(ctx.BlackName, 0, sizeof(ctx.BlackName)); + memset(ctx.type, 0, sizeof(ctx.type)); + memset(ctx.eco, 0, sizeof(ctx.eco)); + memset(ctx.ending, 0, sizeof(ctx.ending)); + memset(ctx.result, 0, sizeof(ctx.result)); + + if (!journal_get_info(&ctx, fname)) return; addjournalitem(p, toupper(save_spot), - WhiteName, WhiteRating, - BlackName, BlackRating, - type, t, i, eco, ending, result, to_file); + ctx.WhiteName, ctx.WhiteRating, + ctx.BlackName, ctx.BlackRating, + ctx.type, + ctx.t, ctx.i, + ctx.eco, + ctx.ending, + ctx.result, + to_file); pprintf(p, "Journal entry %s %c saved in slot %c in journal.\n", parray[p1].name, toupper(from_spot), toupper(save_spot)); } @@ -1814,14 +1845,15 @@ jsave_history(int p, char save_spot, int p1, int from, char *to_file) char *EndSymbol; char *HistoryFname; char *name_to = parray[p].login; - char End[100]; - char command[MAX_FILENAME_SIZE * 2 + 3]; + char End[100] = { '\0' }; + char command[MAX_FILENAME_SIZE * 2 + 3] = { '\0' }; char filename[MAX_FILENAME_SIZE + 1] = { '\0' }; // XXX - char jfname[MAX_FILENAME_SIZE]; + char jfname[MAX_FILENAME_SIZE] = { '\0' }; char type[4]; int g; - if ((HistoryFname = FindHistory2(p, p1, from, End)) != NULL) { + if ((HistoryFname = FindHistory2(p, p1, from, End, sizeof End)) != + NULL) { if ((Game = fopen(HistoryFname, "r")) == NULL) { pprintf(p, "History game %d not available for %s.\n", from, |