cancel
Showing results for 
Search instead for 
Did you mean: 

Bus fault Imprecise error

sirpak
Associate II

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?

13 REPLIES 13

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

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

 

AScha.3
Chief III

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.

If you feel a post has answered your question, please click "Accept as Solution".

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);

Did you check , what malloc giving back? valid address ? s ?

If you feel a post has answered your question, please click "Accept as Solution".
Pavel A.
Evangelist III

Try to increase number of flash wait states?

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

u8 name_length = strlen(objd->name) + 1; // the result is 17
{
    s = (char*)malloc(name_length + 1); // s is 0x20003534
    if(s != NULL)
    {
        p = (u8*)(objd->name); // p is 0x08020C74
        // I tried all these four methods, but they do not work
1.        memcpy(s, p, name_length); 
2.        for(u8 i = 0; i < name_length; i++)
              *(s + i) = *(p + i);
3.        strcpy(s, (objd + nsub)->name);
4.        strlcpy(s, (objd + nsub)->name, name_length);
    }
}

So i am out of ideas... just what i would try:

make heap and stack bigger (double size), to see: any impact - or not.

If you feel a post has answered your question, please click "Accept as Solution".