diff options
author | Markus Uhlin <markus@nifty-networks.net> | 2024-11-23 09:43:55 +0100 |
---|---|---|
committer | Markus Uhlin <markus@nifty-networks.net> | 2024-11-23 09:43:55 +0100 |
commit | b43b7763a35c2b734a02d2cce313f2674970b5e3 (patch) | |
tree | e107549f0ae26e3f15f686249b9ffc931e3b8fee | |
parent | 93925a1a0c36a819294d7f2281260532e3330b6a (diff) |
Improved check_news()
-rw-r--r-- | FICS/command.c | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/FICS/command.c b/FICS/command.c index 88aae2e..e6c76db 100644 --- a/FICS/command.c +++ b/FICS/command.c @@ -738,6 +738,7 @@ rscan_news(FILE *fp, int p, int lc) PRIVATE void check_news(int p, int admin) { +#define SCAN_JUNK "%ld %9s" FILE *fp = NULL; char count[10] = { '\0' }; char filename[MAX_FILENAME_SIZE] = { '\0' }; @@ -747,6 +748,8 @@ check_news(int p, int admin) time_t crtime = 0; time_t lc = player_lastconnect(p); + _Static_assert(ARRAY_SIZE(count) > 9, "Unexpected array size"); + if (admin) { msnprintf(filename, sizeof filename, "%s/newadminnews.index", news_dir); @@ -760,11 +763,23 @@ check_news(int p, int admin) if (num_anews == -1) { num_anews = count_lines(fp); fclose(fp); - fp = fopen(filename, "r"); + if ((fp = fopen(filename, "r")) == NULL) { + warn("%s: can't find admin news index (%s)", + __func__, filename); + return; + } + } + + if (fgets(junk, sizeof junk, fp) == NULL) { + warnx("%s: fgets() error", __func__); + fclose(fp); + return; + } else if (sscanf(junk, SCAN_JUNK, &lval, count) != 2) { + warnx("%s: sscanf() error", __func__); + fclose(fp); + return; } - fgets(junk, MAX_LINE_SIZE, fp); - sscanf(junk, "%ld %s", &lval, count); crtime = lval; if ((crtime - lc) < 0) { @@ -805,8 +820,16 @@ check_news(int p, int admin) } } - fgets(junk, MAX_LINE_SIZE, fp); - sscanf(junk, "%ld %s", &lval, count); + if (fgets(junk, sizeof junk, fp) == NULL) { + warnx("%s: fgets() error", __func__); + fclose(fp); + return; + } else if (sscanf(junk, SCAN_JUNK, &lval, count) != 2) { + warnx("%s: sscanf() error", __func__); + fclose(fp); + return; + } + crtime = lval; if ((crtime - lc) < 0) { |