I am not sure about all the check that are being done by the newlib's _malloc_r implementation, but in your heap_useNewlib_ST.c file, isn't it safer to also add the following check in your _sbrk_r implementation to make sure we do not overflow the currentHeapEnd pointer:
....
DRN_ENTER_CRITICAL_SECTION(usis);
if (incr < 0 && currentHeapEnd + incr > currentHeapEnd ||
incr > 0 && currentHeapEnd + incr < currentHeapEnd) {
// Fail here because currentHeapEnd will be overflown.
}
if (currentHeapEnd + incr > limit) {
...
I am not sure about all the check that are being done by the newlib's
_malloc_rimplementation, but in yourheap_useNewlib_ST.cfile, isn't it safer to also add the following check in your_sbrk_rimplementation to make sure we do not overflow thecurrentHeapEndpointer:.... DRN_ENTER_CRITICAL_SECTION(usis); if (incr < 0 && currentHeapEnd + incr > currentHeapEnd || incr > 0 && currentHeapEnd + incr < currentHeapEnd) { // Fail here because currentHeapEnd will be overflown. } if (currentHeapEnd + incr > limit) { ...