summary refs log tree commit diff
path: root/ui.c
diff options
context:
space:
mode:
authorC. McEnroe2021-06-11 21:02:40 -0400
committerC. McEnroe2021-06-11 21:02:40 -0400
commitc6009cf13c0fd2a2f458d2081c4ed59a07ca0ef0 (patch)
treeec71dffd1ab6b55038dc6a5e02ccdbaa49832efe /ui.c
parent772c9789b785231e235e08af69da89273027cf70 (diff)
Open save file with "a+"
Avoids another small TOCTOU. Rewind before loading since "a+" sets
the file position at the end. Remove unnecessary fseek after
truncation, since "a+" always writes at the end of the file.
Diffstat (limited to 'ui.c')
-rw-r--r--ui.c11
1 files changed, 3 insertions, 8 deletions
diff --git a/ui.c b/ui.c
index 82c4716..22b098e 100644
--- a/ui.c
+++ b/ui.c
@@ -1133,7 +1133,6 @@ static FILE *saveFile;
 int uiSave(void) {
 	int error = 0
 		|| ftruncate(fileno(saveFile), 0)
-		|| fseek(saveFile, 0, SEEK_SET)
 		|| writeTime(saveFile, Signatures[7])
 		|| writeTime(saveFile, self.pos);
 	if (error) return error;
@@ -1180,13 +1179,9 @@ static ssize_t readString(FILE *file, char **buf, size_t *cap) {
 }
 
 void uiLoad(const char *name) {
-	saveFile = dataOpen(name, "r+");
-	if (!saveFile) {
-		if (errno != ENOENT) exit(EX_NOINPUT);
-		saveFile = dataOpen(name, "w");
-		if (!saveFile) exit(EX_CANTCREAT);
-		return;
-	}
+	saveFile = dataOpen(name, "a+");
+	if (!saveFile) exit(EX_CANTCREAT);
+	rewind(saveFile);
 
 	time_t signature;
 	fread(&signature, sizeof(signature), 1, saveFile);