aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-02-16 21:05:28 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-02-16 21:05:28 +0100
commit5e8ac2432d5d1b327be9b8d406b78f008c77bcd5 (patch)
tree2b401a628afe5d4640027bc94ee14b71606b9777
parentb490528bd4097030298bd251fdef2c74a4bfb14b (diff)
Add extra bounds checking
-rw-r--r--src/ahoy/emulator.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/src/ahoy/emulator.c b/src/ahoy/emulator.c
index cffc4a9..f774b54 100644
--- a/src/ahoy/emulator.c
+++ b/src/ahoy/emulator.c
@@ -66,6 +66,8 @@ emuinit(struct u8view prog, const char *fn)
void
emutick(void)
{
+ if ((size_t)c8.PC + 1 >= lengthof(mem))
+ diex("%s: attempted to read instruction beyond end of RAM", filename);
opexec((mem[c8.PC] << 8) | mem[c8.PC + 1]);
c8.PC += 2;
}
@@ -209,8 +211,10 @@ opexec(uint16_t op)
unsigned y = (op & 0x00F0) >> 4;
unsigned n = (op & 0x000F) >> 0;
+ if (c8.I + n > lengthof(mem))
+ diex("%s: attempted to draw sprite beyond bounds of RAM", filename);
+
for (unsigned i = 0; i < n; i++) {
- /* TODO: bounds check? */
uint8_t spr_row = mem[c8.I + i];
uint8_t scr_row = c8.V[y] + i;
uint64_t msk;