diff --git a/src/common/memContext.c b/src/common/memContext.c index ced60451e..f39810989 100644 --- a/src/common/memContext.c +++ b/src/common/memContext.c @@ -749,6 +749,7 @@ memContextNew( ASSERT(param.callbackQty <= 1); // Check context name length ASSERT(name[0] != '\0'); + CHECK(AssertError, memContextMaxStackIdx < MEM_CONTEXT_STACK_MAX - 1, "mem context stack is full"); // Pad allocExtra so that optional structures will be aligned. There may not be any optional structures but it doesn't seem // worth the cost of checking since memory allocations are aligned so the extra bytes would be wasted anyway. @@ -1170,7 +1171,7 @@ memContextSwitch(MemContext *const this) ASSERT(this != NULL); ASSERT(this->active); - ASSERT(memContextCurrentStackIdx < MEM_CONTEXT_STACK_MAX - 1); + CHECK(AssertError, memContextMaxStackIdx < MEM_CONTEXT_STACK_MAX - 1, "mem context stack is full"); memContextMaxStackIdx++; memContextCurrentStackIdx = memContextMaxStackIdx; diff --git a/test/src/module/common/memContextTest.c b/test/src/module/common/memContextTest.c index 0e6a93a20..899c78c6c 100644 --- a/test/src/module/common/memContextTest.c +++ b/test/src/module/common/memContextTest.c @@ -25,6 +25,21 @@ testFree(void *thisVoid) THROW(AssertError, "error in callback"); } +/*********************************************************************************************************************************** +testStackFull - fill the stack with switches and then create or switch to a context +***********************************************************************************************************************************/ +static void +testStackFull(MemContext *const memContext, const bool contextNew) +{ + while (memContextMaxStackIdx < MEM_CONTEXT_STACK_MAX - 1) + memContextSwitch(memContext); + + if (contextNew) + memContextNewP("test-full"); + else + memContextSwitch(memContext); +} + /*********************************************************************************************************************************** Test Run ***********************************************************************************************************************************/ @@ -457,6 +472,12 @@ testRun(void) TEST_RESULT_Z(memContextCurrent()->name, "TOP", "context is now top"); + // Error when the stack is full + TEST_ERROR(testStackFull(memContext, false), AssertError, "mem context stack is full"); + TEST_RESULT_Z(memContextCurrent()->name, "TOP", "context is now top"); + TEST_ERROR(testStackFull(memContext, true), AssertError, "mem context stack is full"); + TEST_RESULT_Z(memContextCurrent()->name, "TOP", "context is now top"); + // Reset temp mem context after a single interaction // ------------------------------------------------------------------------------------------------------------------------- MEM_CONTEXT_TEMP_RESET_BEGIN()