2024-03-12 09:07 AM
Dear Sirs,
I want to edit a string, but the system goes to hard fault (bus fault Imprecise error).
Here below the code:
typedef struct CC_PACKED
{
...
const char *name;
...
} _objd;
static const char acName2005_19[] = "Cycle 0 delta stop";
const _objd SDO2005[] =
{....
{0x19, DTYPE_UNSIGNED32, 32, ATYPE_RW | ATYPE_RXPDO, acName2005_19, 0, 0, MAX_U32, 0, &Modbus_register, C_2005_2006_x_R, C_2005_2006_x_W},
};
in a separate function, since the name is const, I transfer the string to the RAM memory and I try to edit it:
const _objd* objd = ....;
char* s;
u8 name_length = strlen(objd->name) + 1;
s = (char*)malloc(name_length);
strcpy(s, (objd + nsub)->name);
the strcpy sends me in hard fault.
Anybody can please help?
2024-03-12 09:37 AM
Check its not returning a NULL pointer.
Why would you use the length of the string you're copying?
(objd + nsub)->name ?? Is it a string? How long is THIS string?
The error suggests you're writing to the wrong address
>>Anybody can please help?
Debug it. Review the fault in the context of the assembler code immediately prior, the processor registers, and the code being executed.
Add instrumentation to improve your awareness of what's wrong.
2024-03-12 10:24 AM - edited 2024-03-12 10:24 AM
Why would you use the length of the string you're copying?
(objd + nsub)->name ?? Is it a string? How long is THIS string?
The error suggests you're writing to the wrong address
name is a pointer defined as const char *name and it points to a string.
the string length is 20 Bytes approx
2024-03-12 10:41 AM
1. Simple idea : dont use the "const" . Problem away.
2. first : strlen(objd->name) is not same string you copy : (objd + nsub)->name : right ?
so i would use : strncopy( s, (objd->name) , name_length) , to be save from copying too much.
2024-03-12 10:47 AM
please do not consider nsub. the code is a little bit more complex. there is an array of struct and using nsub, i move inside the array.
For simplicity, please do not consider it.
I tried this and it didn't work:
name_length = strlen(objd->name) + 1;
memcpy(s, (u8*)(objd->name), name_length);
2024-03-12 11:04 AM
Did you check , what malloc giving back? valid address ? s ?
2024-03-12 11:04 AM
Try to increase number of flash wait states?
2024-03-12 11:40 AM
I have it already:
FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY_1;
FLASH_ACR_LATENCY_1 is the second bit
Bits 2:0 LATENCY[2:0]: Latency
These bits represent the ratio of the HCLK period to the Flash access time.
000: Zero wait state, if 0 < HCLK ≤ 24 MHz
001: One wait state, if 24 MHz < HCLK ≤ 48 MHz
010: Two wait sates, if 48 < HCLK ≤ 72 MHz
2024-03-12 11:45 AM - edited 2024-03-12 11:47 AM
2024-03-12 12:35 PM
So i am out of ideas... just what i would try:
make heap and stack bigger (double size), to see: any impact - or not.