From 4df6c9241b39bc9103782bc64ea90ae821ee9108 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Tue, 1 Aug 2023 09:37:59 +0200 Subject: make sure we validate pointers returned from zen::Memory::Alloc (#341) * make sure we validate pointers returned from zen::Memory::Alloc --- src/zencore/sharedbuffer.cpp | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'src/zencore/sharedbuffer.cpp') 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 +#include #include #include @@ -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); -- cgit v1.2.3