aboutsummaryrefslogtreecommitdiffstats
path: root/FICS/ratings.c
diff options
context:
space:
mode:
Diffstat (limited to 'FICS/ratings.c')
-rw-r--r--FICS/ratings.c34
1 files changed, 28 insertions, 6 deletions
diff --git a/FICS/ratings.c b/FICS/ratings.c
index e445c51..a719be0 100644
--- a/FICS/ratings.c
+++ b/FICS/ratings.c
@@ -33,6 +33,10 @@
Markus Uhlin 24/11/28 Added null checks
Markus Uhlin 25/03/16 Fixed use of 32-bit 'time_t'.
Markus Uhlin 25/04/06 Fixed Clang Tidy warnings.
+ Markus Uhlin 25/07/28 Fixed missing return-value check
+ for a 'scanf'-like function.
+ Markus Uhlin 25/07/28 Restricted file permissions upon
+ creation.
*/
#include "stdinclude.h"
@@ -40,6 +44,7 @@
#include <err.h>
#include <errno.h>
+#include <fcntl.h>
#include <limits.h>
#include <stdint.h>
@@ -407,12 +412,20 @@ save_ratings(void)
{
FILE *fp;
char fname[MAX_FILENAME_SIZE] = { '\0' };
+ int fd;
snprintf(fname, sizeof fname, "%s/newratingsV%d_data", stats_dir,
STATS_VERSION);
- if ((fp = fopen(fname, "w")) == NULL) {
+ errno = 0;
+ fd = open(fname, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR);
+
+ if (fd < 0) {
+ warn("%s: can't write ratings data", __func__);
+ return;
+ } else if ((fp = fdopen(fd, "w")) == NULL) {
warn("%s: can't write ratings data", __func__);
+ close(fd);
return;
}
@@ -1475,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)
@@ -1488,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;
}
@@ -1567,7 +1589,8 @@ GetRank(FILE *fp, char *target, int countComp)
char line[MAX_RANK_LINE] = { '\0' };
char login[MAX_LOGIN_NAME] = { '\0' };
int count = 0;
- int nGames, is_computer;
+ int is_computer = 0;
+ int nGames = 0;
int playerFound = 0;
while (fgets(line, sizeof line, fp) != NULL &&
@@ -1575,8 +1598,7 @@ GetRank(FILE *fp, char *target, int countComp)
_Static_assert(ARRAY_SIZE(login) > 19, "'login' too small");
if (sscanf(line, "%19s %*d %d %d", login, &nGames, &is_computer)
- != 1) {
-// warnx("%s: sscanf() error", __func__);
+ < 3) {
continue;
}