From 0d27f74bd457689b9930614cfdca1c50d392203f Mon Sep 17 00:00:00 2001 From: Mark Rowe Date: Tue, 12 Nov 2024 15:29:46 -0800 Subject: 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. --- view/sharedcache/core/VM.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'view/sharedcache/core/VM.cpp') 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; -- cgit v1.3.1