diff options
| author | Mark Rowe <mrowe@bdash.net.nz> | 2024-11-12 15:29:46 -0800 |
|---|---|---|
| committer | kat <kat@vector35.com> | 2024-11-13 16:18:10 -0500 |
| commit | 0d27f74bd457689b9930614cfdca1c50d392203f (patch) | |
| tree | dd31badb516139193439b9bdb4074d18169b5a1e /view/sharedcache/core/VM.cpp | |
| parent | f6e5f01b4b1b1e4a5736f9eb78049ae1125b47f8 (diff) | |
Don't leak DataBuffers
`MMappedFileAccessor::ReadBuffer` was returning a heap-allocated
`DataBuffer`, but no callers were ever deleting it. There does not
appear to be any reason to heap allocate the `DataBuffer` as the type is
effectively a smart pointer wrapper around `BNDataBuffer`. Switch to
returning it by value instead.
Additionally, `MMappedFileAccessor::ReadBuffer` was allocating a buffer,
copying data into it, and then handing that allocation to the `DataBuffer`
constructor. The constructor copies data into a new allocation it
owns so this allocation is unnecessary and was being leaked.
Diffstat (limited to 'view/sharedcache/core/VM.cpp')
| -rw-r--r-- | view/sharedcache/core/VM.cpp | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/view/sharedcache/core/VM.cpp b/view/sharedcache/core/VM.cpp index 2e0a39c5..c351a146 100644 --- a/view/sharedcache/core/VM.cpp +++ b/view/sharedcache/core/VM.cpp @@ -453,16 +453,14 @@ int64_t MMappedFileAccessor::ReadLong(size_t address) return ((int64_t*)(&(((uint8_t*)m_mmap._mmap)[address])))[0]; } -BinaryNinja::DataBuffer* MMappedFileAccessor::ReadBuffer(size_t address, size_t length) +BinaryNinja::DataBuffer MMappedFileAccessor::ReadBuffer(size_t address, size_t length) { if (address > m_mmap.len) throw MappingReadException(); if (address + length > m_mmap.len) throw MappingReadException(); void* data = (void*)(&(((uint8_t*)m_mmap._mmap)[address])); - void* dataCopy = malloc(length); - memcpy(dataCopy, data, length); - return new BinaryNinja::DataBuffer(dataCopy, length); + return BinaryNinja::DataBuffer(data, length); } void MMappedFileAccessor::Read(void* dest, size_t address, size_t length) @@ -656,7 +654,7 @@ int64_t VM::ReadLong(size_t address) return mapping.first.fileAccessor->lock()->ReadLong(mapping.second); } -BinaryNinja::DataBuffer* VM::ReadBuffer(size_t addr, size_t length) +BinaryNinja::DataBuffer VM::ReadBuffer(size_t addr, size_t length) { auto mapping = MappingAtAddress(addr); return mapping.first.fileAccessor->lock()->ReadBuffer(mapping.second, length); @@ -767,14 +765,14 @@ size_t VMReader::ReadPointer() return 0; } -BinaryNinja::DataBuffer* VMReader::ReadBuffer(size_t length) +BinaryNinja::DataBuffer VMReader::ReadBuffer(size_t length) { auto mapping = m_vm->MappingAtAddress(m_cursor); m_cursor += length; return mapping.first.fileAccessor->lock()->ReadBuffer(mapping.second, length); } -BinaryNinja::DataBuffer* VMReader::ReadBuffer(size_t addr, size_t length) +BinaryNinja::DataBuffer VMReader::ReadBuffer(size_t addr, size_t length) { auto mapping = m_vm->MappingAtAddress(addr); m_cursor = addr + length; |
