commit c9b06bd

Devine Lu Linvega  ·  2025-01-22 04:04:28 +0000 UTC
parent 45a9c87
(file.c) Moved length checks inside functions
1 files changed,  +14, -18
+14, -18
 1@@ -241,9 +241,13 @@ ensure_parent_dirs(char *p)
 2 }
 3 
 4 static Uint16
 5-file_write(UxnFile *c, void *src, Uint16 len, Uint8 flags)
 6+file_write(UxnFile *c, Uint16 addr, Uint16 len, Uint8 flags)
 7 {
 8+	void *src;
 9 	Uint16 ret = 0;
10+	if(len > 0x10000 - addr)
11+		len = 0x10000 - addr;
12+	src = &uxn.ram[addr];
13 	if(c->outside_sandbox) return 0;
14 	ensure_parent_dirs(c->current_filename);
15 	if(c->state != FILE_WRITE && c->state != DIR_WRITE) {
16@@ -285,9 +289,13 @@ stat_size(Uint8 *dest, Uint16 len, off_t size)
17 }
18 
19 static Uint16
20-file_stat(UxnFile *c, void *dest, Uint16 len)
21+file_stat(UxnFile *c, Uint16 addr, Uint16 len)
22 {
23+	void *dest;
24 	struct stat st;
25+	if(len > 0x10000 - addr)
26+		len = 0x10000 - addr;
27+	dest = &uxn.ram[addr];
28 	if(c->outside_sandbox)
29 		return 0;
30 	else if(stat(c->current_filename, &st))
31@@ -315,10 +323,7 @@ file_deo(Uint8 port)
32 	switch(port) {
33 	case 0xa5:
34 		addr = PEEK2(&uxn.dev[0xa4]);
35-		len = rL1;
36-		if(len > 0x10000 - addr)
37-			len = 0x10000 - addr;
38-		res = file_stat(&uxn_file[0], &uxn.ram[addr], len);
39+		res = file_stat(&uxn_file[0], addr, rL1);
40 		POKE2(&uxn.dev[0xa2], res);
41 		break;
42 	case 0xa6:
43@@ -343,19 +348,13 @@ file_deo(Uint8 port)
44 		break;
45 	case 0xaf:
46 		addr = PEEK2(&uxn.dev[0xae]);
47-		len = rL1;
48-		if(len > 0x10000 - addr)
49-			len = 0x10000 - addr;
50-		res = file_write(&uxn_file[0], &uxn.ram[addr], len, uxn.dev[0xa7]);
51+		res = file_write(&uxn_file[0], addr, rL1, uxn.dev[0xa7]);
52 		POKE2(&uxn.dev[0xa2], res);
53 		break;
54 	/* File 2 */
55 	case 0xb5:
56 		addr = PEEK2(&uxn.dev[0xb4]);
57-		len = rL2;
58-		if(len > 0x10000 - addr)
59-			len = 0x10000 - addr;
60-		res = file_stat(&uxn_file[1], &uxn.ram[addr], len);
61+		res = file_stat(&uxn_file[1], addr, rL2);
62 		POKE2(&uxn.dev[0xb2], res);
63 		break;
64 	case 0xb6:
65@@ -380,10 +379,7 @@ file_deo(Uint8 port)
66 		break;
67 	case 0xbf:
68 		addr = PEEK2(&uxn.dev[0xbe]);
69-		len = rL2;
70-		if(len > 0x10000 - addr)
71-			len = 0x10000 - addr;
72-		res = file_write(&uxn_file[1], &uxn.ram[addr], len, uxn.dev[0xb7]);
73+		res = file_write(&uxn_file[1], addr, rL2, uxn.dev[0xb7]);
74 		POKE2(&uxn.dev[0xb2], res);
75 		break;
76 	}