Skip to content

Commit c6500c2

Browse files
xusheng6claude
andcommitted
[emulator] Mask memory-read-hook value to the buffer width in Python
The Python memory-read hook did result.to_bytes(buf_len, ...), which raises OverflowError if the hook returns a value that does not fit in buf_len bytes; the bare except then swallowed it and silently returned False, dropping a valid hook result. Mask the value to the buffer width first (matching the C++ bridge's truncating behavior) so it is stored instead. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent dbbd4be commit c6500c2

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

plugins/emulator/api/python/ilemulator.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,11 @@ def _cb(ctxt, emu, addr, size, out_buf, buf_len):
393393
try:
394394
result = self._memory_read_hook(self, addr, size)
395395
if result is not None:
396-
raw = result.to_bytes(buf_len, 'little', signed=(result < 0))
396+
# Mask to the buffer width so a value that does not fit (or is negative)
397+
# is truncated to its low bytes instead of raising OverflowError and
398+
# silently dropping the hook result.
399+
mask = (1 << (buf_len * 8)) - 1
400+
raw = (result & mask).to_bytes(buf_len, 'little')
397401
for i in range(buf_len):
398402
out_buf[i] = raw[i]
399403
return True

0 commit comments

Comments
 (0)