From a5b6d8e3913b5d55e58dfbb4e76b0444c1b7b16f Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sun, 4 Jul 2021 00:20:10 +0200 Subject: [PATCH] Rewrite assertion to be more robust. --- m-string.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/m-string.h b/m-string.h index 29c7733b..3bcd5eda 100644 --- a/m-string.h +++ b/m-string.h @@ -270,9 +270,11 @@ m_str1ng_fit2size (string_t v, size_t size_alloc) } M_ASSERT(ptr != &v->u.stack.buffer[0]); if (m_str1ng_stack_p(v)) { - /* Copy the stack allocation into the heap allocation */ - memcpy(ptr, &v->u.stack.buffer[0], - (size_t) v->u.stack.buffer[sizeof (string_heap_ct) - 1] + 1U); + /* Copy the stack allocation into the new heap allocation */ + const size_t size = (size_t) v->u.stack.buffer[sizeof (string_heap_ct) - 1] + 1U; + M_ASSERT( size <= alloc); + M_ASSERT( size <= sizeof (v->u.stack.buffer)); + memcpy(ptr, &v->u.stack.buffer[0], size); } v->ptr = ptr; v->u.heap.alloc = alloc;