memset cause buffer overflow on STM32F4 platform with CMSIS2 RTOS for apparently no reason
Hi everyone,
during my work i have found a strange situation and i want understand what's going on so here i am to finding help. The situation is this: (we are using CMSIS2 api)
There are several task that made different operation.
When an event is triggered a function is called to write in an SPI memory some values.
Before do this an SPI struct (SPI_DATA_BUFFER_STRUCT) is written to 0 using a memset() call.
This memset() cause a buffer overflow for that specific task (the same function is also called from others task without causing any problem).
So the first thing that come in my mind is "ok let's increase the stack size for that task and see if this solve the problem" and effectively this solution solve it but i'm not happy with this so i go to further investigation.
The strange thing is that analyzing the stack memory there is no reason to have a buffer overflow, infact that struct is always all contained in the stack and always at the same distance from the end of the stack. (of course the bytenum for the memset() does not exceed the maximum stack size )
To add another level of strangeness i have tried to move the SPI struct in the global/static memory and i have notice that if the stack size is less than a certain size the task still give the same buffer overflow error.
This let me thinking that there is something that i miss in all this story, so come to the questions:
How can be possible that the memset() cause this problem even if the struct is all contained in the stack when the memset is called for a number of byte that not exceed the stack size and also when come from another memory area (global and static variable declaration)?
and also why that struct is always at the same distance from the end of the stack still when the size of the stack change?
Please let me know if my explanation is not clear in some point or if you need some others information. Thanks in advance to anyone can help with this or also give me some more information on it.
