Skip to content

Commit

Permalink
Fix broken pvPortReallocMicroROS() implementation (#48)
Browse files Browse the repository at this point in the history
The original `pvPortReallocMicroROS()` implementation copies more bytes than expected, which causes memory error when using functions like `rosidl_runtime_c__String__assignn()`.

(cherry picked from commit 5b627a1)
  • Loading branch information
yushijinhun authored and mergify-bot committed Mar 9, 2022
1 parent c41c562 commit a438f18
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions extra_sources/custom_memory_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

#include <stdlib.h>
#include <string.h>

/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
all the API functions to use the MPU wrappers. That should only be done when
Expand Down Expand Up @@ -293,22 +294,17 @@ void *pvPortReallocMicroROS( void *pv, size_t xWantedSize )
vTaskSuspendAll();

void * newmem = pvPortMallocMicroROS(xWantedSize);
if (newmem != NULL)
{
size_t count = getBlockSize(pv) - xHeapStructSize;
if (xWantedSize < count)
{
count = xWantedSize;
}
memcpy(newmem, pv, count);

uint8_t *puc = ( uint8_t * ) pv;
BlockLink_t *pxLink;

puc -= xHeapStructSize;
pxLink = ( void * ) puc;


char *in_src = (char*)pv;
char *in_dest = (char*)newmem;
size_t count = pxLink->xBlockSize & ~xBlockAllocatedBit;

while(count--)
*in_dest++ = *in_src++;

vPortFreeMicroROS(pv);
vPortFreeMicroROS(pv);
}

( void ) xTaskResumeAll();

Expand Down Expand Up @@ -456,4 +452,4 @@ uint8_t *puc;
{
mtCOVERAGE_TEST_MARKER();
}
}
}

0 comments on commit a438f18

Please sign in to comment.