Skip to content

Wrong address validation during Memory access #60

@GermanCodeEngineer

Description

@GermanCodeEngineer

Describe the bug
A clear and concise description of what the bug is.

In memory.py the bounds check uses a chained comparison if 0x0000 < address > self.size:. That expression means (0x0000 < address) and (address > self.size). As written it does not reject negative indices or address == self.size, so invalid addresses can slip through (leading to Python negative indexing or IndexError later) instead of raising ValueError.

To Reproduce
Steps to reproduce the behavior:

  1. Open a Python REPL in the project root.
  2. Run:
    from src.m6502.memory import Memory
    m = Memory()          # default size
    m[-1]                 # negative index should be invalid
  3. Observe that m[-1] accesses the last element (due to Python negative indexing) instead of raising ValueError.
  4. Also run:
    m[m.size]             # index equal to size should be invalid
  5. Observe inconsistent behavior (may raise IndexError instead of ValueError).

Expected behavior
A clear and concise description of what you expected to happen.

Reading or writing an address outside the valid range should always raise ValueError("Memory address is not valid"). Use a proper two-sided range check so only addresses in [0x0000, self.size - 1] are accepted.

Additional context
Add any other context about the problem here.

Location: memory.py - the incorrect checks appear in __getitem__ and __setitem__.

Suggested fix: replace the faulty line in both methods with:

# accept only addresses in [0x0000, self.size-1]
if (address < 0x0000) or (address >= self.size):
    raise ValueError("Memory address is not valid")

This ensures negative indices and addresses >= self.size are rejected consistently.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions