aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Uhlin <markus@nifty-networks.net>2024-07-18 01:42:26 +0200
committerMarkus Uhlin <markus@nifty-networks.net>2024-07-18 01:42:26 +0200
commitd10bb2013313f025057899de4b4df563cf11ed7b (patch)
treef66221375c8138cb26f115b54c802b9d100a2f3a
parent9d3b5040e833831f83d9a03729cd08353b1ac0f5 (diff)
RemoveHistGame: check the return of fgets() and sscanf()
-rw-r--r--FICS/gamedb.c19
1 files changed, 14 insertions, 5 deletions
diff --git a/FICS/gamedb.c b/FICS/gamedb.c
index 2c4511b..4f58ddf 100644
--- a/FICS/gamedb.c
+++ b/FICS/gamedb.c
@@ -1543,17 +1543,26 @@ RemoveHistGame(char *file, int maxlines)
{
FILE *fp;
char GameFile[MAX_FILENAME_SIZE] = { '\0' };
- char Opponent[MAX_LOGIN_NAME] = { '\0' };
+ char Opponent[MAX_LOGIN_NAME + 1] = { '\0' };
char line[MAX_LINE_SIZE] = { '\0' };
int count = 0;
long int When, oppWhen;
- if ((fp = fopen(file, "r")) == NULL)
+ _Static_assert(20 < ARRAY_SIZE(Opponent));
+
+ if ((fp = fopen(file, "r")) == NULL) {
+ return;
+ } else if (fgets(line, ARRAY_SIZE(line), fp) == NULL) {
+ warnx("%s: fgets error (file: %s)", __func__, file);
+ fclose(fp);
return;
+ } else if (sscanf(line, "%*d %*c %*d %*c %*d %20s %*s %*d %*d %*d "
+ "%*d %*s %*s %ld", Opponent, &When) != 2) {
+ warnx("%s: unexpected initial line (file: %s)", __func__, file);
+ fclose(fp);
+ return;
+ }
- fgets(line, ARRAY_SIZE(line), fp);
- sscanf(line, "%*d %*c %*d %*c %*d %s %*s %*d %*d %*d %*d %*s %*s %ld",
- Opponent, &When);
count++;
while (!feof(fp)) {