aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/sharedbuffer.cpp
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/zencore/sharedbuffer.cpp
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/zencore/sharedbuffer.cpp')
-rw-r--r--src/zencore/sharedbuffer.cpp30
1 files changed, 22 insertions, 8 deletions
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);