diff options
Diffstat (limited to 'FICS')
-rw-r--r-- | FICS/adminproc.c | 42 | ||||
-rw-r--r-- | FICS/command.c | 9 | ||||
-rw-r--r-- | FICS/comproc.c | 30 | ||||
-rw-r--r-- | FICS/gamedb.c | 11 | ||||
-rw-r--r-- | FICS/playerdb.c | 70 | ||||
-rw-r--r-- | FICS/ratings.c | 13 |
6 files changed, 127 insertions, 48 deletions
diff --git a/FICS/adminproc.c b/FICS/adminproc.c index e670d44..dfe528e 100644 --- a/FICS/adminproc.c +++ b/FICS/adminproc.c @@ -19,6 +19,8 @@ #include <sys/param.h> #include <err.h> +#include <errno.h> +#include <fcntl.h> #include <inttypes.h> #include <stdint.h> @@ -173,6 +175,7 @@ create_news_file(int p, param_list param, int admin) { FILE *fp; char filename[MAX_FILENAME_SIZE] = { '\0' }; + int fd; ASSERT(parray[p].adminLevel >= ADMIN_ADMIN); @@ -185,10 +188,14 @@ create_news_file(int p, param_list param, int admin) msnprintf(filename, sizeof filename, "%s/adminnews.%d", news_dir, param[0].val.integer); - if ((fp = fopen(filename, "w")) != NULL) { + fd = open(filename, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR); + if (fd < 0) + return COM_FAILED; + else if ((fp = fdopen(fd, "w")) != NULL) { fprintf(fp, "%s\n", param[1].val.string); fclose(fp); - } + } else + close(fd); } } else { if (param[0].val.integer > num_news) { @@ -198,10 +205,14 @@ create_news_file(int p, param_list param, int admin) msnprintf(filename, sizeof filename, "%s/news.%d", news_dir, param[0].val.integer); - if ((fp = fopen(filename, "w")) != NULL) { + fd = open(filename, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR); + if (fd < 0) + return COM_FAILED; + else if ((fp = fdopen(fd, "w")) != NULL) { fprintf(fp, "%s\n", param[1].val.string); fclose(fp); - } + } else + close(fd); } } @@ -214,11 +225,19 @@ add_item(char *new_item, char *filename) FILE *new_fp, *old_fp; char junk[MAX_LINE_SIZE] = { '\0' }; char tmp_file[MAX_FILENAME_SIZE] = { '\0' }; + int fd; msnprintf(tmp_file, sizeof tmp_file, "%s/.tmp.idx", news_dir); - if ((new_fp = fopen(tmp_file, "w")) == NULL) + fd = open(tmp_file, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR); + + if (fd < 0) + return 0; + else if ((new_fp = fdopen(fd, "w")) == NULL) { + close(fd); return 0; + } + fprintf(new_fp, "%s", new_item); if ((old_fp = fopen(filename, "r")) == NULL) @@ -373,6 +392,7 @@ com_anews(int p, param_list param) char filename[MAX_FILENAME_SIZE] = { '\0' }; char junk[MAX_LINE_SIZE] = { '\0' }; char *junkp = NULL; + const char *v_scan_junk = "%" SCNd64 " " "%9s"; int found = 0; int64_t lval = 0; time_t crtime = 0; @@ -384,7 +404,6 @@ com_anews(int p, param_list param) return COM_OK; } -#define SCAN_JUNK ("%" SCNd64 " " "%9s") _Static_assert(9 < ARRAY_SIZE(count), "Array too small"); if (param[0].type == 0) { @@ -402,7 +421,7 @@ com_anews(int p, param_list param) fclose(fp); return COM_FAILED; } - if (sscanf(junk, SCAN_JUNK, &lval, count) != 2) { + if (sscanf(junk, v_scan_junk, &lval, count) != 2) { warnx("%s: sscanf() error: too few items", __func__); fclose(fp); return COM_FAILED; @@ -431,7 +450,7 @@ com_anews(int p, param_list param) fclose(fp); return COM_FAILED; } - if (sscanf(junk, SCAN_JUNK, &lval, count) != 2) { + if (sscanf(junk, v_scan_junk, &lval, count) != 2) { warnx("%s: sscanf() error: too few items", __func__); fclose(fp); return COM_FAILED; @@ -455,8 +474,11 @@ com_anews(int p, param_list param) break; if (strlen(junk) > 1) { - if (sscanf(junkp, SCAN_JUNK, &lval, count) != 2) - warnx("%s: sscanf() error...", __func__); + if (sscanf(junkp, v_scan_junk, &lval, + count) != 2) { + warnx("%s: sscanf() error...", + __func__); + } crtime = lval; diff --git a/FICS/command.c b/FICS/command.c index 301c305..bf69bf0 100644 --- a/FICS/command.c +++ b/FICS/command.c @@ -722,6 +722,7 @@ rscan_news(FILE *fp, int p, time_t lc) char count[10] = { '\0' }; char junk[MAX_LINE_SIZE] = { '\0' }; char *junkp = NULL; + const char *scan_fmt = "%" SCNd64 " " "%9s"; int64_t lval = 0; time_t crtime = 0; @@ -731,7 +732,7 @@ rscan_news(FILE *fp, int p, time_t lc) _Static_assert(ARRAY_SIZE(count) > 9, "Unexpected array size"); - if (sscanf(junk, ("%" SCNd64 " " "%9s"), &lval, count) != 2) { + if (sscanf(junk, scan_fmt, &lval, count) != 2) { warnx("%s: sscanf() error: too few items", __func__); return; } @@ -755,12 +756,12 @@ rscan_news(FILE *fp, int p, time_t lc) PRIVATE void check_news(int p, int admin) { -#define SCAN_JUNK ("%" SCNd64 " " "%9s") FILE *fp = NULL; char count[10] = { '\0' }; char filename[MAX_FILENAME_SIZE] = { '\0' }; char junk[MAX_LINE_SIZE] = { '\0' }; char *junkp = NULL; + const char *v_scan_fmt = "%" SCNd64 " " "%9s"; int64_t lval = 0; time_t crtime = 0; time_t lc = player_lastconnect(p); @@ -791,7 +792,7 @@ check_news(int p, int admin) warnx("%s: fgets() error", __func__); fclose(fp); return; - } else if (sscanf(junk, SCAN_JUNK, &lval, count) != 2) { + } else if (sscanf(junk, v_scan_fmt, &lval, count) != 2) { warnx("%s: sscanf() error", __func__); fclose(fp); return; @@ -841,7 +842,7 @@ check_news(int p, int admin) warnx("%s: fgets() error", __func__); fclose(fp); return; - } else if (sscanf(junk, SCAN_JUNK, &lval, count) != 2) { + } else if (sscanf(junk, v_scan_fmt, &lval, count) != 2) { warnx("%s: sscanf() error", __func__); fclose(fp); return; diff --git a/FICS/comproc.c b/FICS/comproc.c index a7a5ea1..b2504fd 100644 --- a/FICS/comproc.c +++ b/FICS/comproc.c @@ -47,6 +47,7 @@ result converted to larger type. Markus Uhlin 25/07/24 Fixed use of potentially dangerous functions. + Markus Uhlin 25/07/29 Usage of 'int64_t'. */ #include "stdinclude.h" @@ -56,6 +57,8 @@ #include <err.h> #include <errno.h> +#include <inttypes.h> +#include <stdint.h> #include "board.h" #include "command.h" @@ -130,17 +133,18 @@ com_more(int p, param_list param) PUBLIC void rscan_news2(FILE *fp, int p, int num) { - char *junkp; char count[10] = { '\0' }; char junk[MAX_LINE_SIZE] = { '\0' }; - long int lval; + char *junkp; + const char *v_scan_fmt = "%" SCNd64 " " "%9s"; + int64_t lval; time_t crtime; if (num == 0) return; if (fgets(junk, sizeof junk, fp) == NULL || feof(fp) || - sscanf(junk, "%ld %9s", &lval, count) != 2) + sscanf(junk, v_scan_fmt, &lval, count) != 2) return; rscan_news2(fp, p, num - 1); @@ -157,12 +161,13 @@ PUBLIC int com_news(int p, param_list param) { FILE *fp = NULL; - char *junkp = NULL; char count[10] = { '\0' }; char filename[MAX_FILENAME_SIZE] = { '\0' }; char junk[MAX_LINE_SIZE] = { '\0' }; + char *junkp = NULL; + const char *v_scan_fmt = "%" SCNd64 " " "%9s"; int found = 0; - long int lval = 0; + int64_t lval = 0; time_t crtime = 0; snprintf(filename, sizeof filename, "%s/newnews.index", news_dir); @@ -172,7 +177,6 @@ com_news(int p, param_list param) return COM_OK; } -#define SCAN_JUNK "%ld %9s" _Static_assert(9 < ARRAY_SIZE(count), "'count' too small"); if (param[0].type == 0) { @@ -184,7 +188,7 @@ com_news(int p, param_list param) pprintf(p, "Index of recent news items:\n"); if (fgets(junk, sizeof junk, fp) == NULL || - sscanf(junk, SCAN_JUNK, &lval, count) != 2) { + sscanf(junk, v_scan_fmt, &lval, count) != 2) { warnx("%s: error: fgets() or sscanf()", __func__); fclose(fp); return COM_FAILED; @@ -209,7 +213,7 @@ com_news(int p, param_list param) pprintf(p, "Index of all news items:\n"); if (fgets(junk, sizeof junk, fp) == NULL || - sscanf(junk, SCAN_JUNK, &lval, count) != 2) { + sscanf(junk, v_scan_fmt, &lval, count) != 2) { warnx("%s: error: fgets() or sscanf()", __func__); fclose(fp); return COM_FAILED; @@ -235,7 +239,7 @@ com_news(int p, param_list param) if (fgets(junk, sizeof junk, fp) == NULL || feof(fp)) break; - if (sscanf(junkp, SCAN_JUNK, &lval, count) != 2) + if (sscanf(junkp, v_scan_fmt, &lval, count) != 2) warnx("%s: sscanf() error...", __func__); crtime = lval; @@ -762,8 +766,10 @@ plogins(int p, char *fname) FILE *fp = NULL; char ipstr[20] = { '\0' }; char loginName[MAX_LOGIN_NAME + 1] = { '\0' }; + const char *v_scan_fmt = "%" SCNu16 " %19s " "%" SCNd64 " " + "%d %19s\n"; int registered = 0; - long int lval = 0; + int64_t lval = 0; time_t tval = 0; uint16_t inout = 0; @@ -776,8 +782,8 @@ plogins(int p, char *fname) _Static_assert(19 < ARRAY_SIZE(loginName), "'loginName' too small"); while (!feof(fp)) { - if (fscanf(fp, "%hu %19s %ld %d %19s\n", &inout, loginName, - &lval, ®istered, ipstr) != 5) { + if (fscanf(fp, v_scan_fmt, &inout, loginName, &lval, + ®istered, ipstr) != 5) { fprintf(stderr, "FICS: Error in login info format. " "%s\n", fname); fclose(fp); diff --git a/FICS/gamedb.c b/FICS/gamedb.c index 7f5e645..19f0dc7 100644 --- a/FICS/gamedb.c +++ b/FICS/gamedb.c @@ -45,6 +45,7 @@ Markus Uhlin 25/04/06 Fixed Clang Tidy warnings. Markus Uhlin 25/07/28 Fixed use of potentially dangerous functions. + Markus Uhlin 25/07/29 Usage of 'int64_t'. */ #include "stdinclude.h" @@ -52,7 +53,9 @@ #include <err.h> #include <errno.h> +#include <inttypes.h> #include <limits.h> +#include <stdint.h> #include "command.h" #include "config.h" @@ -1282,7 +1285,7 @@ PRIVATE int ReadV1GameFmt(game *g, FILE *fp, const char *file, int version) { int ret[3]; - long int lval; + int64_t lval; _Static_assert(17 < ARRAY_SIZE(g->white_name), "Unexpected array size"); _Static_assert(17 < ARRAY_SIZE(g->black_name), "Unexpected array size"); @@ -1306,7 +1309,7 @@ ReadV1GameFmt(game *g, FILE *fp, const char *file, int version) if (version < 3 && !g->bInitTime) g->bInitTime = g->wInitTime; - if (fscanf(fp, "%ld", &lval) != 1) { + if (fscanf(fp, "%" SCNd64, &lval) != 1) { warnx("%s: %s: failed to get time of start", __func__, file); return -1; } else @@ -1520,7 +1523,7 @@ PRIVATE void WriteGameFile(FILE *fp, int g) { game *gg = &garray[g]; - long int lval; + int64_t lval; player *bp = &parray[gg->black]; player *wp = &parray[gg->white]; @@ -1531,7 +1534,7 @@ WriteGameFile(FILE *fp, int g) gg->bInitTime, gg->bIncrement); lval = gg->timeOfStart; - fprintf(fp, "%ld\n", lval); + fprintf(fp, "%" PRId64 "\n", lval); #ifdef TIMESEAL fprintf(fp, "%d %d\n", diff --git a/FICS/playerdb.c b/FICS/playerdb.c index 2f8055f..29e14f4 100644 --- a/FICS/playerdb.c +++ b/FICS/playerdb.c @@ -48,6 +48,7 @@ Markus Uhlin 25/04/06 Fixed Clang Tidy warnings. Markus Uhlin 25/07/28 Restricted file permissions upon creation. + Markus Uhlin 25/07/30 Usage of 'int64_t'. */ #include "stdinclude.h" @@ -1622,10 +1623,20 @@ player_ontime(int p) PRIVATE void write_p_inout(int inout, int p, char *file, int maxlines) { - FILE *fp; + FILE *fp; + int fd; - if ((fp = fopen(file, "a")) == NULL) + errno = 0; + fd = open(file, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR); + + if (fd < 0) { + warn("%s: open", __func__); + return; + } else if ((fp = fdopen(fd, "a")) == NULL) { + warn("%s: fdopen", __func__); + close(fd); return; + } fprintf(fp, "%d %s %ld %d %s\n", inout, parray[p].name, (long int)time(NULL), parray[p].registered, @@ -1684,7 +1695,7 @@ player_lastconnect(int p) char loginName[MAX_LOGIN_NAME]; int inout, registered; int ret, too_long; - long int lval = 0; + int64_t lval = 0; time_t last = 0; ret = snprintf(fname, sizeof fname, "%s/player_data/%c/%s.%s", @@ -1710,8 +1721,8 @@ player_lastconnect(int p) _Static_assert(19 < ARRAY_SIZE(ipstr), "'ipstr' too small"); - if (fscanf(fp, "%d %19s %ld %d %19s\n", &inout, loginName, - &lval, ®istered, ipstr) != 5) { + if (fscanf(fp, ("%d %19s " "%" SCNd64 " %d %19s\n"), &inout, + loginName, &lval, ®istered, ipstr) != 5) { fprintf(stderr, "FICS: Error in login info format. %s" "\n", fname); fclose(fp); @@ -1732,7 +1743,7 @@ player_lastdisconnect(int p) char loginName[MAX_LOGIN_NAME]; int inout, registered; int ret, too_long; - long int lval; + int64_t lval; time_t last = 0; ret = snprintf(fname, sizeof fname, "%s/player_data/%c/%s.%s", @@ -1753,8 +1764,8 @@ player_lastdisconnect(int p) _Static_assert(19 < ARRAY_SIZE(ipstr), "'ipstr' too small"); - if (fscanf(fp, "%d %19s %ld %d %19s\n", &inout, loginName, - &lval, ®istered, ipstr) != 5) { + if (fscanf(fp, ("%d %19s " "%" SCNd64 " %d %19s\n"), &inout, + loginName, &lval, ®istered, ipstr) != 5) { fprintf(stderr, "FICS: Error in login info format. %s" "\n", fname); fclose(fp); @@ -2594,9 +2605,10 @@ PUBLIC int player_add_message(int top, int fromp, char *message) { FILE *fp; - char fname[MAX_FILENAME_SIZE]; - char messbody[1024]; - char subj[256]; + char fname[MAX_FILENAME_SIZE] = { '\0' }; + char messbody[1024] = { '\0' }; + char subj[256] = { '\0' }; + int fd; time_t t = time(NULL); if (!parray[top].registered) @@ -2609,8 +2621,16 @@ player_add_message(int top, int fromp, char *message) if (lines_file(fname) >= MAX_MESSAGES && parray[top].adminLevel == 0) return -1; - if ((fp = fopen(fname, "a")) == NULL) + errno = 0; + fd = open(fname, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR); + + if (fd < 0) + return -1; + else if ((fp = fdopen(fd, "a")) == NULL) { + close(fd); return -1; + } + fprintf(fp, "%s at %s: %s\n", parray[fromp].name, strltime(&t), message); fclose(fp); @@ -2773,13 +2793,21 @@ PRIVATE int WriteMsgFile(int p, textlist *Head) { FILE *fp; - char fName[MAX_FILENAME_SIZE]; + char fName[MAX_FILENAME_SIZE] = { '\0' }; + int fd; textlist *Cur; GetMsgFile(p, fName, sizeof fName, __func__); - if ((fp = fopen(fName, "w")) == NULL) + errno = 0; + fd = open(fName, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR); + + if (fd < 0) return 0; + else if ((fp = fdopen(fd, "w")) == NULL) { + close(fd); + return 0; + } for (Cur = Head; Cur != NULL; Cur = Cur->next) fprintf(fp, "%s", Cur->text); fclose(fp); @@ -3182,7 +3210,8 @@ PUBLIC int player_add_comment(int p_by, int p_to, char *comment) { FILE *fp; - char fname[MAX_FILENAME_SIZE]; + char fname[MAX_FILENAME_SIZE] = { '\0' }; + int fd; time_t t = time(NULL); if (!parray[p_to].registered) @@ -3191,8 +3220,17 @@ player_add_comment(int p_by, int p_to, char *comment) snprintf(fname, sizeof fname, "%s/player_data/%c/%s.%s", stats_dir, parray[p_to].login[0], parray[p_to].login, "comments"); - if ((fp = fopen(fname, "a")) == NULL) + errno = 0; + fd = open(fname, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR); + + if (fd < 0) { + warn("%s: open", __func__); + return -1; + } else if ((fp = fdopen(fd, "a")) == NULL) { + warn("%s: fdopen", __func__); + close(fd); return -1; + } fprintf(fp, "%s at %s: %s\n", parray[p_by].name, strltime(&t), comment); fclose(fp); diff --git a/FICS/ratings.c b/FICS/ratings.c index 2f82e62..a719be0 100644 --- a/FICS/ratings.c +++ b/FICS/ratings.c @@ -1488,7 +1488,8 @@ UpdateRank(int type, char *addName, statistics *sNew, char *delName) char command[MAX_STRING_LENGTH]; char line[MAX_RANK_LINE] = { '\0' }; char login[MAX_LOGIN_NAME] = { '\0' }; - int comp; + int comp = 0; + int fd = -1; statistics sCur; if (GetRankFileName(RankFile, sizeof RankFile, type) < 0) @@ -1501,9 +1502,17 @@ UpdateRank(int type, char *addName, statistics *sNew, char *delName) snprintf(TmpRankFile, sizeof TmpRankFile, "%s/tmpRank", sdir); - if ((fptemp = fopen(TmpRankFile, "w")) == NULL) { + errno = 0; + fd = open(TmpRankFile, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR); + + if (fd < 0) { + warn("%s: open", __func__); + fclose(fp); + return; + } else if ((fptemp = fdopen(fd, "w")) == NULL) { warn("%s: unable to open rank file for updating", __func__); fclose(fp); + close(fd); return; } |