aboutsummaryrefslogtreecommitdiff
path: root/unprotbas.c
diff options
context:
space:
mode:
authorB. Watson <urchlay@slackware.uk>2024-05-19 15:54:13 -0400
committerB. Watson <urchlay@slackware.uk>2024-05-19 15:54:13 -0400
commit1583dac116be07cf98145c95397e19d8c8e7b833 (patch)
tree857b72727efac8ae6c9bc5d8deeb09222fe7c4b2 /unprotbas.c
parent626d5d99b6a5e94fe04d5a6a7a8270a4c4536344 (diff)
downloadbw-atari8-tools-1583dac116be07cf98145c95397e19d8c8e7b833.tar.gz
unprotbas: check for bad memmove and too-small input file.
Diffstat (limited to 'unprotbas.c')
-rw-r--r--unprotbas.c17
1 files changed, 16 insertions, 1 deletions
diff --git a/unprotbas.c b/unprotbas.c
index 0b11e0e..16fbea5 100644
--- a/unprotbas.c
+++ b/unprotbas.c
@@ -16,6 +16,12 @@
#define STM_OFFSET 0xf2
+/* minimum program size, for a program that has no variables and
+ only one line of code (the immediate line 32768, consisting only of
+ one token, which would be CSAVE). anything smaller than this, we
+ can't process. */
+#define MIN_PROG_SIZE 21
+
/* entire file gets read into memory (for now) */
unsigned char data[65536];
@@ -60,6 +66,8 @@ int readfile(void) {
int got = fread(data, 1, 65535, input_file);
if(verbose) fprintf(stderr, "read %d bytes\n", got);
fclose(input_file);
+ if(got < MIN_PROG_SIZE)
+ die("File too short to be a BASIC program (truncated?)\n");
return got;
}
@@ -194,7 +202,14 @@ int fixcode(void) {
by moving the rest of the program (including the variable value
table) up in memory. */
void move_code(int offset) {
- memmove(data + vvstart + offset, data + vvstart, filelen);
+ unsigned char *dest = data + vvstart + offset;
+
+ if(dest < data || (filelen + offset) > 65535) {
+ die("attempt to move memory out of range; corrupt header bytes?\n");
+ }
+
+ memmove(dest, data + vvstart, filelen);
+
vntd += offset;
vvtp += offset;
stmtab += offset;