Skip to content

Commit

Permalink
fix bug in readMemString(<>, wide=True) where the last \x00 is consid…
Browse files Browse the repository at this point in the history
…ered part of the terminator instead of the last character (vivisect#663)

with unittests
  • Loading branch information
atlas0fd00m authored Aug 19, 2024
1 parent 4729907 commit e29a86b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions envi/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,10 @@ def readMemString(self, va, maxlen=0xfffffff, wide=False):

# now find the end of the string based on either \x00, maxlen, or end of map
end = mbytes.find(terminator, offset)
while (end > offset) and (end-offset) % len(terminator) != 0:
# with codepage 0, we really need \0\0\0 because the first
# 0 is part of the last character
end = mbytes.find(terminator, end+1)

left = end - offset
if end == -1:
Expand Down
14 changes: 14 additions & 0 deletions envi/tests/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,18 @@ def test_allocator(self):
with self.assertRaises(e_exc.NoValidFreeMemoryFound):
failmap = mem.allocateMemory(0x100000)

def test_readMemString(self):
mem = e_mem.MemoryObject()
mem.addMemoryMap(0x41410000, e_const.MM_RWX, 'test', b'\0'*1024)
mem.addMemoryMap(0x41420000, e_const.MM_RWX, 'test', b'abcdefghijklmnop\0' + b'\0'*1024)
mem.addMemoryMap(0x41430000, e_const.MM_RWX, 'test', b'a\0b\0c\0d\0e\0f\0g\0h\0i\0j\0k\0l\0m\0n\0o\0p\0' + b'\0'*1024)

self.assertEqual(mem.readMemString(0x41410000), b'')
self.assertEqual(mem.readMemString(0x41420000), b'abcdefghijklmnop')
self.assertEqual(mem.readMemString(0x41430000), b'a')

self.assertEqual(mem.readMemString(0x41410000, wide=True), b'',)
self.assertEqual(mem.readMemString(0x41420000, wide=True), b'abcdefghijklmnop') # only looks for '\0\0' terminator
self.assertEqual(mem.readMemString(0x41430000, wide=True), b'a\0b\0c\0d\0e\0f\0g\0h\0i\0j\0k\0l\0m\0n\0o\0p\0')


0 comments on commit e29a86b

Please sign in to comment.