FreeRTOS+CLI Command List Corruped by FreeRTOS_inet_ntoa()

I have an STM32F7 Nucleo and I am trying to use the TCP CLI component from the STM32F4 example from FreeRTOSLabs. It simly has the ip-config and netstat commands. The problem I am having is that in the ip-config command callback, there is a call as follows:

FreeRTOS_inet_ntoa( ulAddress, ( &( pcWriteBuffer[ strlen( pcWriteBuffer ) ] ) ) ); 

So in the telnet console, when I call the ip-config, the Ip address is printed correctly, but on the second round of the callback, before it should print the Netmask, there is a Hard Fault. What I can see is that the first ntoa call somehow overwrites the CLI list in memory, so when

xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput );

is called there is an invalid address for the Ip Config pxCommandLineDefinintion

Here is what I see before and after FreeRTOS_inet_ntoa() in the debugger:

Any suggestions on why this might happen? I am using BufferAllocation_2 for the TCP stack and heap_4 for MemMang. I tried increasing the heap size but it didn’t fix the problem. I’m guessing part of the problem is somehow in the pvPortMalloc but I cant find whats going on.

FreeRTOS_inet_ntoa() calls sprintf(), so my first thought is that sprintf() is causing corruption somewhere.

Where is your implementation of sprintf() coming from? If it is part of a standard GCC library then it is likely it will use a lot of stack space and sometimes might even call malloc(). First, do you have stack overflow protection turned on? If not, turn in on by setting configCHECK_FOR_STACK_OVERFLOW to 2. You can also try increasing the stack size of the task calling sprintf() to see if that makes any difference. Next, unless you are using heap_3.c, in which case you will have a heap defined by your C runtime and calling malloc() will be ok, try defining your own copy of malloc() and then put an assert into it to make double sure nothing is calling it. If you are using anything other than heap_3.c then there is not necessarily a heap available for malloc() as FreeRTOS uses pvPortMalloc().

void *malloc( size_t x )
    /* Force an assert. */
    configASSERT( x == 0 );
1 Like

Yes increasing the stack size fixed the problem. After reading your link it becomes clear what was going on. It also clears up some of the questions on had about memory Managment so thank you!

1 Like

@orangesalad thank you for reporting back. I’ve marked this as solved with @rtel post on your behalf.

1 Like