commit 60d5252

d_m  ·  2025-03-08 03:47:51 +0000 UTC
parent e156ecc
Warn when filename is uninitialized.

Previously, writing to some file device ports before specifying
a filename could cause a segmentation fault. After this change,
a warning will be printed to stderr and the operation will
immediately fail (writing an appopriate value to File/success).
1 files changed,  +18, -0
+18, -0
 1@@ -167,10 +167,22 @@ file_init(int id, Uint16 addr)
 2 	return 0;
 3 }
 4 
 5+static int
 6+file_not_ready(int id)
 7+{
 8+	if(ufs[id].filepath == 0) {
 9+		fprintf(stderr, "File %d is uninitialized\n", id);
10+		return 1;
11+	} else
12+		return 0;
13+}
14+
15 static int
16 file_read(int id, Uint16 addr, int len)
17 {
18 	void *dest = &uxn.ram[addr];
19+	if(file_not_ready(id))
20+		return 0;
21 	if(ufs[id].state != FILE_READ && ufs[id].state != DIR_READ) {
22 		file_reset(id);
23 		if((ufs[id].dir = opendir(ufs[id].filepath)) != NULL)
24@@ -189,6 +201,8 @@ static int
25 file_write(int id, Uint16 addr, int len, Uint8 flags)
26 {
27 	int ret = 0;
28+	if(file_not_ready(id))
29+		return 0;
30 	file_write_dir(ufs[id].filepath);
31 	if(ufs[id].state != FILE_WRITE && ufs[id].state != DIR_WRITE) {
32 		file_reset(id);
33@@ -209,6 +223,8 @@ static int
34 file_stat(int id, Uint16 addr, int len)
35 {
36 	struct stat st;
37+	if(file_not_ready(id))
38+		return 0;
39 	int err = stat(ufs[id].filepath, &st), dir = S_ISDIR(st.st_mode);
40 	return put_stat(&uxn.ram[addr], len, st.st_size, err, dir, 0);
41 }
42@@ -216,6 +232,8 @@ file_stat(int id, Uint16 addr, int len)
43 static int
44 file_delete(int id)
45 {
46+	if(file_not_ready(id))
47+		return -1;
48 	return unlink(ufs[id].filepath);
49 }
50