2024-07-01 03:55 AM - edited 2024-07-02 12:19 AM
Hi,
I'm facing a problem in the managing of local variables on STM32WB50CG with the STM32CubeIDE version 1.14.1.
Specifically, the code must perform a write via the sprintf function on a local variable (allocated at address 0x20009bd8).
ALL the writings are performed correctly but during the execution of the same sprintf, a memory area in which a global variable is allocated (address 0x20009708) is also overwritten with the same data without generating any interrupt in MemManage_Handler.
At the end of the execution of DiagnosticPktCreator I obtained the correct string inside the datafifo but the same string is also present at address 0x20009708.
Any ideas on why this occurs?
void DiagnosticPacketGenerator(void){
char datafifo[MAX_PACKET_SIZE];
uint16_t fifosize;
static uint32_t PTimeOld_Diagnostic;
uint8_t i = 0;
memset(datafifo,0,MAX_PACKET_SIZE);
PTimeOld_Diagnostic=(HAL_GetTick()/1000)-PTimeOld_Diagnostic;
while(i < GetSystemVariable().ValvesNumber)
{
/*Diagnostic packet after update (8) or direct reply (0)*/
if(GetDiagnosticEnable()==3)
DiagnosticPktCreator(&datafifo[0], &fifosize, 8, PTimeOld_Diagnostic, &i);
else
DiagnosticPktCreator(&datafifo[0], &fifosize, 0, PTimeOld_Diagnostic, &i);
FIFO_Packet_handler_opt(FIFO_PUSH, (uint8_t*)&datafifo[0], &fifosize);
i++;
}
PTimeOld_Diagnostic=(HAL_GetTick())/1000;
}
void DiagnosticPktCreator(char* data, uint16_t* data_size, uint8_t PacketType, uint32_t PTime, uint8_t* idx){
strcpy(data,"{pkt_9:");
uint16_t packet_len;
/*function that adds informations to the packet*/
Key_DB(&data[strlen(data)],PacketType, PTime);
Identifying_DB(&data[strlen(data)]);
Diagnostic_DB(&data[strlen(data)]);
strcpy(&data[strlen(data)],"}}");
*data_size=strlen(data);
}
void Key_DB(char* data, uint8_t PacketType, uint32_t Time ){ sprintf(data,"{\"key\":\"%u,%s,%lu,%s,%u,%u\"",GetSystemVariable().PacketCounter,GetSystemVariable().Datetime,Time,GetConnection_info().IMEI,GetSystemVariable().Bootcode,PacketType);
}
void Identifying_DB(char* data){
sprintf(data,",\"id\":\"%s,%s\"",GetConnection_info().ICCID,GetConnection_info().Cell_info);
}
void Diagnostic_DB(char* data){
sprintf(data,",\"diagnostic\":\"%u,%s,%.f,%u,%s\"",GetSystemVariable().EDStatus,GetConnection_info().Cell_pwr,GetSystem_Acquisition().Voltage,GetSystem_Acquisition().Frequency,FW_VERSION);
}
Thank you in advance.
Davide Graziani
2024-07-01 04:03 AM
stack overflow?
buffer overrun?
bad pointers?
You're going to need to show the code!
2024-07-01 04:28 AM - edited 2024-07-01 04:45 AM
Not entirely sure what you're describing..
The top of the statics, locale, heap and the bottom of the stack can collide.You can perhaps watch the stack size/depth and check heap integrity.
You'd need/want to enable stack checking options, check malloc() returns, and perhaps understand the linked-lists the C libraries are using so you can walk them.
MemManage / HardFault only likely to fire in gross situations, you can do a lot of damage sliently and without warnings.
Perhap add some guard zones and check their integrity periodically.
2024-07-02 12:20 AM - edited 2024-07-02 12:20 AM
Thank you for the advice, it is my first question. I add the code snippet in the question.
Davide Graziani
2024-07-02 12:33 AM
Tank you for the reply.
"The top of the statics, locale, heap and the bottom of the stack can collide" If this occurs i think that i will see only one string that overwrites other variables, in this case i obtain the correct value in the correct memory address and also an exact copy of the variable allocated in another memory address.
"You'd need/want to enable stack checking options, check malloc() returns" I cannot check the malloc() return because i don't use any dynamic allocation.
I add the code in the question if you want to look at it.
Davide Graziani