From fd55cb43a4d4e0019603b1fab9a4f82068ec594b Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 25 Mar 2022 09:46:47 +0100 Subject: [PATCH] Fix pvPortRealloc (#99) (#100) * Fix pvPortRealloc * Add check for NULL input pointer * Modify if to one-liner (cherry picked from commit d00d81ab7f930f28a162841e29e62fc1eda0d7a4) Co-authored-by: Antonio Cuadros <49162117+Acuadros95@users.noreply.github.com> --- .../src/custom_memory_manager.c | 25 +++++++++++-------- .../Core/Src/custom_memory_manager.c | 23 ++++++++++------- .../Src/custom_memory_manager.c | 23 ++++++++++------- .../Src/custom_memory_manager.c | 23 ++++++++++------- .../Src/custom_memory_manager.c | 23 ++++++++++------- .../Src/custom_memory_manager.c | 23 +++++++++-------- 6 files changed, 83 insertions(+), 57 deletions(-) diff --git a/microros_crazyflie21_extensions/src/custom_memory_manager.c b/microros_crazyflie21_extensions/src/custom_memory_manager.c index 924dc503..c04a76ff 100644 --- a/microros_crazyflie21_extensions/src/custom_memory_manager.c +++ b/microros_crazyflie21_extensions/src/custom_memory_manager.c @@ -300,21 +300,25 @@ void *pvPortRealloc( void *pv, size_t xWantedSize ) void * newmem = pvPortMalloc(xWantedSize); - uint8_t *puc = ( uint8_t * ) pv; - BlockLink_t *pxLink; + if (newmem != NULL && pv != NULL) + { + uint8_t *puc = ( uint8_t * ) pv; + BlockLink_t *pxLink; - puc -= xHeapStructSize; - pxLink = ( void * ) puc; + puc -= xHeapStructSize; + pxLink = ( void * ) puc; - char *in_src = (char*)pv; - char *in_dest = (char*)newmem; - size_t count = pxLink->xBlockSize & ~xBlockAllocatedBit; + char *in_src = (char*)pv; + char *in_dest = (char*)newmem; - while(count--) - *in_dest++ = *in_src++; + size_t count = (pxLink->xBlockSize & ~xBlockAllocatedBit) - xHeapStructSize; + if (xWantedSize < count) count = xWantedSize; - vPortFree(pv); + while (count--) *in_dest++ = *in_src++; + + vPortFree(pv); + } ( void ) xTaskResumeAll(); @@ -463,4 +467,3 @@ uint8_t *puc; mtCOVERAGE_TEST_MARKER(); } } - diff --git a/microros_nucleo_f446re_extensions/Core/Src/custom_memory_manager.c b/microros_nucleo_f446re_extensions/Core/Src/custom_memory_manager.c index 5605064c..683680fc 100644 --- a/microros_nucleo_f446re_extensions/Core/Src/custom_memory_manager.c +++ b/microros_nucleo_f446re_extensions/Core/Src/custom_memory_manager.c @@ -265,19 +265,24 @@ void *pvPortRealloc(void *pv, size_t xWantedSize) { void *newmem = pvPortMalloc(xWantedSize); - uint8_t *puc = (uint8_t *)pv; - BlockLink_t *pxLink; + if (newmem != NULL && pv != NULL) + { + uint8_t *puc = (uint8_t *)pv; + BlockLink_t *pxLink; - puc -= xHeapStructSize; - pxLink = (void *)puc; + puc -= xHeapStructSize; + pxLink = (void *)puc; - char *in_src = (char *)pv; - char *in_dest = (char *)newmem; - size_t count = pxLink->xBlockSize & ~xBlockAllocatedBit; + char *in_src = (char *)pv; + char *in_dest = (char *)newmem; + + size_t count = (pxLink->xBlockSize & ~xBlockAllocatedBit) - xHeapStructSize; + if (xWantedSize < count) count = xWantedSize; - while (count--) *in_dest++ = *in_src++; + while (count--) *in_dest++ = *in_src++; - vPortFree(pv); + vPortFree(pv); + } (void)xTaskResumeAll(); diff --git a/microros_nucleo_f446ze_extensions/Src/custom_memory_manager.c b/microros_nucleo_f446ze_extensions/Src/custom_memory_manager.c index 5605064c..683680fc 100644 --- a/microros_nucleo_f446ze_extensions/Src/custom_memory_manager.c +++ b/microros_nucleo_f446ze_extensions/Src/custom_memory_manager.c @@ -265,19 +265,24 @@ void *pvPortRealloc(void *pv, size_t xWantedSize) { void *newmem = pvPortMalloc(xWantedSize); - uint8_t *puc = (uint8_t *)pv; - BlockLink_t *pxLink; + if (newmem != NULL && pv != NULL) + { + uint8_t *puc = (uint8_t *)pv; + BlockLink_t *pxLink; - puc -= xHeapStructSize; - pxLink = (void *)puc; + puc -= xHeapStructSize; + pxLink = (void *)puc; - char *in_src = (char *)pv; - char *in_dest = (char *)newmem; - size_t count = pxLink->xBlockSize & ~xBlockAllocatedBit; + char *in_src = (char *)pv; + char *in_dest = (char *)newmem; + + size_t count = (pxLink->xBlockSize & ~xBlockAllocatedBit) - xHeapStructSize; + if (xWantedSize < count) count = xWantedSize; - while (count--) *in_dest++ = *in_src++; + while (count--) *in_dest++ = *in_src++; - vPortFree(pv); + vPortFree(pv); + } (void)xTaskResumeAll(); diff --git a/microros_nucleo_f746zg_extensions/Src/custom_memory_manager.c b/microros_nucleo_f746zg_extensions/Src/custom_memory_manager.c index 5605064c..2509bab4 100644 --- a/microros_nucleo_f746zg_extensions/Src/custom_memory_manager.c +++ b/microros_nucleo_f746zg_extensions/Src/custom_memory_manager.c @@ -265,19 +265,24 @@ void *pvPortRealloc(void *pv, size_t xWantedSize) { void *newmem = pvPortMalloc(xWantedSize); - uint8_t *puc = (uint8_t *)pv; - BlockLink_t *pxLink; + if (newmem != NULL && pv != NULL) + { + uint8_t *puc = (uint8_t *)pv; + BlockLink_t *pxLink; - puc -= xHeapStructSize; - pxLink = (void *)puc; + puc -= xHeapStructSize; + pxLink = (void *)puc; - char *in_src = (char *)pv; - char *in_dest = (char *)newmem; - size_t count = pxLink->xBlockSize & ~xBlockAllocatedBit; + char *in_src = (char *)pv; + char *in_dest = (char *)newmem; + + size_t count = (pxLink->xBlockSize & ~xBlockAllocatedBit) - xHeapStructSize; + if (xWantedSize < count) count = xWantedSize; - while (count--) *in_dest++ = *in_src++; + while (count--) *in_dest++ = *in_src++; - vPortFree(pv); + vPortFree(pv); + } (void)xTaskResumeAll(); diff --git a/microros_nucleo_f767zi_extensions/Src/custom_memory_manager.c b/microros_nucleo_f767zi_extensions/Src/custom_memory_manager.c index 5605064c..683680fc 100644 --- a/microros_nucleo_f767zi_extensions/Src/custom_memory_manager.c +++ b/microros_nucleo_f767zi_extensions/Src/custom_memory_manager.c @@ -265,19 +265,24 @@ void *pvPortRealloc(void *pv, size_t xWantedSize) { void *newmem = pvPortMalloc(xWantedSize); - uint8_t *puc = (uint8_t *)pv; - BlockLink_t *pxLink; + if (newmem != NULL && pv != NULL) + { + uint8_t *puc = (uint8_t *)pv; + BlockLink_t *pxLink; - puc -= xHeapStructSize; - pxLink = (void *)puc; + puc -= xHeapStructSize; + pxLink = (void *)puc; - char *in_src = (char *)pv; - char *in_dest = (char *)newmem; - size_t count = pxLink->xBlockSize & ~xBlockAllocatedBit; + char *in_src = (char *)pv; + char *in_dest = (char *)newmem; + + size_t count = (pxLink->xBlockSize & ~xBlockAllocatedBit) - xHeapStructSize; + if (xWantedSize < count) count = xWantedSize; - while (count--) *in_dest++ = *in_src++; + while (count--) *in_dest++ = *in_src++; - vPortFree(pv); + vPortFree(pv); + } (void)xTaskResumeAll(); diff --git a/microros_olimex_e407_extensions/Src/custom_memory_manager.c b/microros_olimex_e407_extensions/Src/custom_memory_manager.c index 2eac0b57..c2c8fac7 100644 --- a/microros_olimex_e407_extensions/Src/custom_memory_manager.c +++ b/microros_olimex_e407_extensions/Src/custom_memory_manager.c @@ -300,21 +300,24 @@ void *pvPortRealloc( void *pv, size_t xWantedSize ) void * newmem = pvPortMalloc(xWantedSize); - uint8_t *puc = ( uint8_t * ) pv; - BlockLink_t *pxLink; + if (newmem != NULL && pv != NULL) + { + uint8_t *puc = ( uint8_t * ) pv; + BlockLink_t *pxLink; - puc -= xHeapStructSize; - pxLink = ( void * ) puc; + puc -= xHeapStructSize; + pxLink = ( void * ) puc; + char *in_src = (char*)pv; + char *in_dest = (char*)newmem; - char *in_src = (char*)pv; - char *in_dest = (char*)newmem; - size_t count = pxLink->xBlockSize & ~xBlockAllocatedBit; + size_t count = (pxLink->xBlockSize & ~xBlockAllocatedBit) - xHeapStructSize; + if (xWantedSize < count) count = xWantedSize; - while(count--) - *in_dest++ = *in_src++; + while (count--) *in_dest++ = *in_src++; - vPortFree(pv); + vPortFree(pv); + } ( void ) xTaskResumeAll();