Keep a local copy of File/name; don't try to read it beyond end of RAM.

This commit is contained in:
Andrew Alderwick 2022-03-17 18:41:55 +00:00
parent 27089a77e4
commit 205cc89bcb
1 changed files with 13 additions and 5 deletions

View File

@ -22,7 +22,7 @@ WITH REGARD TO THIS SOFTWARE.
typedef struct { typedef struct {
FILE *f; FILE *f;
DIR *dir; DIR *dir;
char *current_filename; char current_filename[4096];
struct dirent *de; struct dirent *de;
enum { IDLE, enum { IDLE,
FILE_READ, FILE_READ,
@ -66,7 +66,7 @@ get_entry(char *p, Uint16 len, const char *pathname, const char *basename, int f
static Uint16 static Uint16
file_read_dir(UxnFile *c, char *dest, Uint16 len) file_read_dir(UxnFile *c, char *dest, Uint16 len)
{ {
static char pathname[4096]; static char pathname[4352];
char *p = dest; char *p = dest;
if(c->de == NULL) c->de = readdir(c->dir); if(c->de == NULL) c->de = readdir(c->dir);
for(; c->de != NULL; c->de = readdir(c->dir)) { for(; c->de != NULL; c->de = readdir(c->dir)) {
@ -86,10 +86,18 @@ file_read_dir(UxnFile *c, char *dest, Uint16 len)
} }
static Uint16 static Uint16
file_init(UxnFile *c, void *filename) file_init(UxnFile *c, char *filename, size_t max_len)
{ {
char *p = c->current_filename;
size_t len = sizeof(c->current_filename);
reset(c); reset(c);
c->current_filename = filename; if(len > max_len) len = max_len;
while(len) {
if((*p++ = *filename++) == '\0')
return 0;
len--;
}
c->current_filename[0] = '\0';
return 0; return 0;
} }
@ -165,7 +173,7 @@ file_i_deo(int instance, Device *d, Uint8 port)
break; break;
case 0x9: case 0x9:
DEVPEEK16(addr, 0x8); DEVPEEK16(addr, 0x8);
res = file_init(c, (char *)&d->u->ram[addr]); res = file_init(c, (char *)&d->u->ram[addr], 0x10000 - addr);
DEVPOKE16(0x2, res); DEVPOKE16(0x2, res);
break; break;
case 0xd: case 0xd: