commit c6a7b38
Devie Lu Linvega
·
2025-12-23 20:13:24 +0000 UTC
parent fcacc46
Catch overflow in File device
3 files changed,
+25,
-3
+12,
-3
1@@ -7,17 +7,19 @@
2
3 @on-reset ( -> )
4 #800f DEO
5- ;dict/create file/test-create <test>
6+ ;dict/write file/test-write <test>
7 ;dict/append file/test-append <test>
8 ;dict/read file/test-read <test>
9 ;dict/stat file/test-stat <test>
10 ;dict/delete file/test-delete <test>
11+ ( | overflows )
12+ ;dict/write-of file/test-write-of <test>
13 BRK
14
15 (
16 @|Tests )
17
18-@file/test-create ( -- pass )
19+@file/test-write ( -- pass )
20 ;&name .File/name DEO2
21 #0002 .File/length DEO2
22 ;&a1 .File/write DEO2
23@@ -57,6 +59,12 @@
24 ;&null-buf .File/stat DEO2
25 ;&null-buf LDA2 LIT2 "!! EQU2 AND JMP2r
26
27+@file/test-write-of ( -- pass )
28+ ;&name .File/name DEO2
29+ #0004 .File/length DEO2
30+ #fffe .File/write DEO2
31+ .File/success DEI2 #0002 EQU2 JMP2r
32+
33 (
34 @|Helpers )
35
36@@ -77,11 +85,12 @@
37 (
38 @|Assets )
39
40-@dict/create "File/create: 20 $1
41+@dict/write "File/write: 20 $1
42 &append "File/append: 20 $1
43 &read "File/read: 20 $1
44 &stat "File/stat: 20 $1
45 &delete "File/delete: 20 $1
46+ &write-of "File/write(overflow): 20 $1
47 &fail "fail 0a $1
48 &pass "pass 0a $1
49
+7,
-0
1@@ -712,6 +712,7 @@ mouse_scroll(Uint16 x, Uint16 y)
2
3 #include <dirent.h>
4 #include <sys/stat.h>
5+#include <string.h>
6
7 typedef struct {
8 FILE *f;
9@@ -876,6 +877,8 @@ file_read(unsigned int id, Uint16 addr, unsigned int len)
10 void *dest = &ram[addr];
11 if(file_not_ready(id))
12 return 0;
13+ if(addr + len > 0x10000)
14+ len = 0x10000 - addr;
15 if(ufs[id].state != FILE_READ && ufs[id].state != DIR_READ) {
16 file_reset(id);
17 if((ufs[id].dir = opendir(ufs[id].filepath)) != NULL)
18@@ -896,6 +899,8 @@ file_write(unsigned int id, Uint16 addr, unsigned int len, Uint8 flags)
19 unsigned int ret = 0;
20 if(file_not_ready(id))
21 return 0;
22+ if(addr + len > 0x10000)
23+ len = 0x10000 - addr;
24 file_write_dir(ufs[id].filepath);
25 if(ufs[id].state != FILE_WRITE && ufs[id].state != DIR_WRITE) {
26 file_reset(id);
27@@ -919,6 +924,8 @@ file_stat(unsigned int id, Uint16 addr, unsigned int len)
28 struct stat st;
29 if(file_not_ready(id))
30 return 0;
31+ if(addr + len > 0x10000)
32+ len = 0x10000 - addr;
33 err = stat(ufs[id].filepath, &st);
34 dir = S_ISDIR(st.st_mode);
35 return put_stat(&ram[addr], len, st.st_size, err, dir, 0);
+6,
-0
1@@ -536,6 +536,8 @@ file_read(unsigned int id, Uint16 addr, unsigned int len)
2 void *dest = &ram[addr];
3 if(file_not_ready(id))
4 return 0;
5+ if(addr + len > 0x10000)
6+ len = 0x10000 - addr;
7 if(ufs[id].state != FILE_READ && ufs[id].state != DIR_READ) {
8 file_reset(id);
9 if((ufs[id].dir = opendir(ufs[id].filepath)) != NULL)
10@@ -556,6 +558,8 @@ file_write(unsigned int id, Uint16 addr, unsigned int len, Uint8 flags)
11 unsigned int ret = 0;
12 if(file_not_ready(id))
13 return 0;
14+ if(addr + len > 0x10000)
15+ len = 0x10000 - addr;
16 file_write_dir(ufs[id].filepath);
17 if(ufs[id].state != FILE_WRITE && ufs[id].state != DIR_WRITE) {
18 file_reset(id);
19@@ -579,6 +583,8 @@ file_stat(unsigned int id, Uint16 addr, unsigned int len)
20 struct stat st;
21 if(file_not_ready(id))
22 return 0;
23+ if(addr + len > 0x10000)
24+ len = 0x10000 - addr;
25 err = stat(ufs[id].filepath, &st);
26 dir = S_ISDIR(st.st_mode);
27 return put_stat(&ram[addr], len, st.st_size, err, dir, 0);