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:
- Open a Python REPL in the project root.
- Run:
from src.m6502.memory import Memory
m = Memory() # default size
m[-1] # negative index should be invalid
- Observe that
m[-1] accesses the last element (due to Python negative indexing) instead of raising ValueError.
- Also run:
m[m.size] # index equal to size should be invalid
- 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.
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 oraddress == self.size, so invalid addresses can slip through (leading to Python negative indexing or IndexError later) instead of raisingValueError.To Reproduce
Steps to reproduce the behavior:
m[-1]accesses the last element (due to Python negative indexing) instead of raisingValueError.IndexErrorinstead ofValueError).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:
This ensures negative indices and addresses >=
self.sizeare rejected consistently.