From d78c22842f4ef76383c4a0e0013ac63fa7a6a942 Mon Sep 17 00:00:00 2001 From: Erik Pilkington Date: Mon, 23 Jul 2018 22:23:04 +0000 Subject: [demangler] call terminate() if allocation failed We really should set *status to memory_alloc_failure, but we need to refactor the demangler a bit to properly propagate the failure up the stack. Until then, its better to explicitly terminate then rely on a null dereference crash. rdar://31240372 --- libcxxabi/src/cxa_demangle.cpp | 17 +++++++++++++---- libcxxabi/src/demangle/Utility.h | 4 ++++ 2 files changed, 17 insertions(+), 4 deletions(-) (limited to 'libcxxabi') diff --git a/libcxxabi/src/cxa_demangle.cpp b/libcxxabi/src/cxa_demangle.cpp index 78d667117fd..9a2c83a9bec 100644 --- a/libcxxabi/src/cxa_demangle.cpp +++ b/libcxxabi/src/cxa_demangle.cpp @@ -1771,13 +1771,17 @@ class BumpPointerAllocator { BlockMeta* BlockList = nullptr; void grow() { - char* NewMeta = new char[AllocSize]; + char* NewMeta = static_cast(std::malloc(AllocSize)); + if (NewMeta == nullptr) + std::terminate(); BlockList = new (NewMeta) BlockMeta{BlockList, 0}; } void* allocateMassive(size_t NBytes) { NBytes += sizeof(BlockMeta); - BlockMeta* NewMeta = reinterpret_cast(new char[NBytes]); + BlockMeta* NewMeta = reinterpret_cast(std::malloc(NBytes)); + if (NewMeta == nullptr) + std::terminate(); BlockList->Next = new (NewMeta) BlockMeta{BlockList->Next, 0}; return static_cast(NewMeta + 1); } @@ -1803,7 +1807,7 @@ public: BlockMeta* Tmp = BlockList; BlockList = BlockList->Next; if (reinterpret_cast(Tmp) != InitialBuffer) - delete[] reinterpret_cast(Tmp); + std::free(Tmp); } BlockList = new (InitialBuffer) BlockMeta{nullptr, 0}; } @@ -1833,10 +1837,15 @@ class PODSmallVector { size_t S = size(); if (isInline()) { auto* Tmp = static_cast(std::malloc(NewCap * sizeof(T))); + if (Tmp == nullptr) + std::terminate(); std::copy(First, Last, Tmp); First = Tmp; - } else + } else { First = static_cast(std::realloc(First, NewCap * sizeof(T))); + if (First == nullptr) + std::terminate(); + } Last = First + S; Cap = First + NewCap; } diff --git a/libcxxabi/src/demangle/Utility.h b/libcxxabi/src/demangle/Utility.h index a424c23b8ad..39092436283 100644 --- a/libcxxabi/src/demangle/Utility.h +++ b/libcxxabi/src/demangle/Utility.h @@ -35,6 +35,8 @@ class OutputStream { if (BufferCapacity < N + CurrentPosition) BufferCapacity = N + CurrentPosition; Buffer = static_cast(std::realloc(Buffer, BufferCapacity)); + if (Buffer == nullptr) + std::terminate(); } } @@ -76,6 +78,8 @@ public: if (!StartBuf || !Size) { StartBuf = static_cast(std::malloc(AllocSize)); + if (StartBuf == nullptr) + std::terminate(); Size = &AllocSize; } -- cgit v1.2.3