From 1c9262735a87ec6f029e4a875f77dd9f7634c31f Mon Sep 17 00:00:00 2001 From: Julien Vanegue Date: Mon, 18 Mar 2024 15:41:08 -0400 Subject: [PATCH] Fixing an infinite loop scenario in case of memory exhaustion Throwing an exception after 10 attempts rather than trying to allocate indefinitely (which could be infinite) --- allocate.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/allocate.cpp b/allocate.cpp index 1cb42d9de..fcb9d34cf 100644 --- a/allocate.cpp +++ b/allocate.cpp @@ -39,6 +39,7 @@ void CallNewHandler() void * AlignedAllocate(size_t size) { + unsigned int cnt = 0; byte *p; #if defined(CRYPTOPP_MM_MALLOC_AVAILABLE) while ((p = (byte *)_mm_malloc(size, 16)) == NULLPTR) @@ -51,8 +52,12 @@ void * AlignedAllocate(size_t size) #else while ((p = (byte *)malloc(size + 16)) == NULLPTR) #endif - CallNewHandler(); - + { + if (cnt >= 10) + throw std::bad_alloc(); + CallNewHandler(); + cnt++; + } #ifdef CRYPTOPP_NO_ALIGNED_ALLOC size_t adjustment = 16-((size_t)p%16); CRYPTOPP_ASSERT(adjustment > 0);