Validate instruction addresses and memory bounds in the emulator

Open Beginner friendly
#7 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp

Research direction

Start in emulator/main.c, in the instruction decoding loop identified in the issue. Trace how BASE, instruction values, and computed addresses are used before memory reads, then check the emulator's existing build or run instructions. Done when malformed bases, addresses, and instructions stop safely with descriptive errors and valid programs still work.

Written by the indexing model from the issue text.

Description

bug copilot

Summary

The emulator derives memory addresses from instruction values without validating them, which can read beyond the allocated memory and crash or corrupt state.

Problem

The code computes address = memory[i] % BASE and reads memory[address] for every instruction. If the program or base value is malformed, address may be outside the bounds of the memory array. Similarly, the instruction stream itself is not validated before decoding.

Expected behavior

  • Check BASE and address ranges before reading memory.
  • Reject instructions that are invalid for the current architecture.
  • Stop safely with a descriptive error on malformed programs.

Relevant code

  • emulator/main.c in the for loop that decodes instructions and accesses memory.

Suggested fix

Add range checks before each memory read and each address calculation. Reject BASE <= 0 and guard memory accesses with 0 <= address && address < MEMORY_SIZE before dereferencing them. Report invalid instructions instead of falling through to undefined behavior.

Dominant language
C++
Stars
0
Forks
0
Avg merge
1m
Merged PRs (30d)
4

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from DavidSkillman/LittleManCompiler

All issues in DavidSkillman/LittleManCompiler

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.