Skip to main content
Associate II
Sep 12, 2024 4:25 PM UTC
Solved

LWIP_RAND uses newlib rand() and fails

CubeMX generated project defines

 

#define LWIP_RAND() ((u32_t)rand())

 

which fails with the following call stack...

Gustavo_AR_1-1726157473699.png

and the following assert text

assertion "REENT malloc succeeded" failed: file "/build/gnu-tools-for-stm32_12.3.rel1.20240612-1315/src/newlib/newlib/libc/stdlib/rand.c", line 82

Line 82 of rand.c is:

 

_REENT_CHECK_RAND48(reent);

 

which in turn ends up calling a malloc... 

I've read that reentrancy in newlib could cause problems, but I thought it would be memory corruption.

Is there a solution for this?

Thanks in advance!

This topic has been closed for replies.
Imen.DBest answer
ST Technical Moderator
Sep 12, 2024 7:28 PM UTC

Hello @Gustavo_AR ,

Seems you have the same issue reported in these posts:

Hope the shared posts will help you, otherwise please share more details about the device, the tools version used and how to reproduce the issue.

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks

2 replies

Imen.DBest answer
ST Technical Moderator
Sep 12, 2024 7:28 PM UTC

Hello @Gustavo_AR ,

Seems you have the same issue reported in these posts:

Hope the shared posts will help you, otherwise please share more details about the device, the tools version used and how to reproduce the issue.

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
Associate II
Sep 13, 2024 8:59 PM UTC

Thanks @Imen.D I've fixed it by 

/* Defining malloc/free should overwrite the standard versions provided by the compiler. */

void *malloc(size_t size) {
 /* Call the FreeRTOS version of malloc. */
 return pvPortMalloc(size);
}

void free(void *ptr) {
 /* Call the FreeRTOS version of free. */
 vPortFree(ptr);
}