aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2023-08-01 09:37:59 +0200
committerGitHub <[email protected]>2023-08-01 09:37:59 +0200
commit4df6c9241b39bc9103782bc64ea90ae821ee9108 (patch)
treedfc69f41b544cf818e5821ccf9e6c60cf01d759b /src
parentadd requested item in oplog remote op (#340) (diff)
downloadzen-4df6c9241b39bc9103782bc64ea90ae821ee9108.tar.xz
zen-4df6c9241b39bc9103782bc64ea90ae821ee9108.zip
make sure we validate pointers returned from zen::Memory::Alloc (#341)
* make sure we validate pointers returned from zen::Memory::Alloc
Diffstat (limited to 'src')
-rw-r--r--src/zen/internalfile.cpp4
-rw-r--r--src/zencore/memory.cpp10
-rw-r--r--src/zencore/sharedbuffer.cpp30
3 files changed, 34 insertions, 10 deletions
diff --git a/src/zen/internalfile.cpp b/src/zen/internalfile.cpp
index 3b3d466a1..5cfd98d9e 100644
--- a/src/zen/internalfile.cpp
+++ b/src/zen/internalfile.cpp
@@ -229,6 +229,10 @@ InternalFile::MemoryMapFile()
if (FileSize <= 100 * 1024 * 1024)
{
m_Memory = zen::Memory::Alloc(FileSize, 64);
+ if (!m_Memory)
+ {
+ zen::ThrowOutOfMemory(fmt::format("failed allocating {:#x} bytes aligned to {:#x}", FileSize, 64));
+ }
Read(m_Memory, FileSize, 0);
return m_Memory;
diff --git a/src/zencore/memory.cpp b/src/zencore/memory.cpp
index 1f148cede..546296b10 100644
--- a/src/zencore/memory.cpp
+++ b/src/zencore/memory.cpp
@@ -1,5 +1,7 @@
// Copyright Epic Games, Inc. All Rights Reserved.
+#include <zencore/except.h>
+#include <zencore/fmtutils.h>
#include <zencore/intmath.h>
#include <zencore/memory.h>
#include <zencore/testing.h>
@@ -133,8 +135,12 @@ ChunkingLinearAllocator::Alloc(size_t Size, size_t Alignment)
{
const uint64_t ChunkSize = zen::RoundUp(zen::Max(m_ChunkSize, Size), m_ChunkSize);
void* ChunkPtr = Memory::Alloc(ChunkSize, m_ChunkAlignment);
- m_ChunkCursor = reinterpret_cast<uint8_t*>(ChunkPtr);
- m_ChunkBytesRemain = ChunkSize;
+ if (!ChunkPtr)
+ {
+ ThrowOutOfMemory(fmt::format("failed allocating {:#x} bytes aligned to {:#x}", ChunkSize, m_ChunkAlignment));
+ }
+ m_ChunkCursor = reinterpret_cast<uint8_t*>(ChunkPtr);
+ m_ChunkBytesRemain = ChunkSize;
m_ChunkList.push_back(ChunkPtr);
}
diff --git a/src/zencore/sharedbuffer.cpp b/src/zencore/sharedbuffer.cpp
index 200e06972..993ca40e6 100644
--- a/src/zencore/sharedbuffer.cpp
+++ b/src/zencore/sharedbuffer.cpp
@@ -1,5 +1,7 @@
// Copyright Epic Games, Inc. All Rights Reserved.
+#include <zencore/except.h>
+#include <zencore/fmtutils.h>
#include <zencore/sharedbuffer.h>
#include <zencore/testing.h>
@@ -15,8 +17,12 @@ namespace zen {
UniqueBuffer
UniqueBuffer::Alloc(uint64_t Size)
{
- void* Buffer = Memory::Alloc(Size, 16);
- IoBufferCore* Owner = new IoBufferCore(Buffer, Size);
+ void* Buffer = Memory::Alloc(Size, 16);
+ if (!Buffer)
+ {
+ ThrowOutOfMemory(fmt::format("failed allocating {:#x} bytes aligned to {:#x}", Size, 16));
+ }
+ IoBufferCore* Owner = new IoBufferCore(Buffer, Size);
Owner->SetIsOwnedByThis(true);
Owner->SetIsImmutable(false);
@@ -107,9 +113,13 @@ SharedBuffer::MakeView(const void* Data, uint64_t Size)
SharedBuffer
SharedBuffer::Clone()
{
- const uint64_t Size = GetSize();
- void* Buffer = Memory::Alloc(Size, 16);
- auto NewOwner = new IoBufferCore(Buffer, Size);
+ const uint64_t Size = GetSize();
+ void* Buffer = Memory::Alloc(Size, 16);
+ if (!Buffer)
+ {
+ ThrowOutOfMemory(fmt::format("failed allocating {:#x} bytes aligned to {:#x}", Size, 16));
+ }
+ auto NewOwner = new IoBufferCore(Buffer, Size);
NewOwner->SetIsOwnedByThis(true);
memcpy(Buffer, m_Buffer->DataPointer(), Size);
@@ -119,9 +129,13 @@ SharedBuffer::Clone()
SharedBuffer
SharedBuffer::Clone(MemoryView View)
{
- const uint64_t Size = View.GetSize();
- void* Buffer = Memory::Alloc(Size, 16);
- auto NewOwner = new IoBufferCore(Buffer, Size);
+ const uint64_t Size = View.GetSize();
+ void* Buffer = Memory::Alloc(Size, 16);
+ if (!Buffer)
+ {
+ ThrowOutOfMemory(fmt::format("failed allocating {:#x} bytes aligned to {:#x}", Size, 16));
+ }
+ auto NewOwner = new IoBufferCore(Buffer, Size);
NewOwner->SetIsOwnedByThis(true);
memcpy(Buffer, View.GetData(), Size);