Skip to main content
sirpak
Associate III
March 12, 2024
Question

Bus fault Imprecise error

  • March 12, 2024
  • 4 replies
  • 2895 views

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?

4 replies

Tesla DeLorean
Guru
March 12, 2024

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 VenmoUp vote any posts that you find helpful, it shows what's working..
sirpak
sirpakAuthor
Associate III
March 12, 2024

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
Super User
March 12, 2024

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""."
sirpak
sirpakAuthor
Associate III
March 12, 2024

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

AScha.3
Super User
March 12, 2024

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.
Super User
March 12, 2024

Try to increase number of flash wait states?

sirpak
sirpakAuthor
Associate III
March 12, 2024

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

Pavel A.
Super User
March 12, 2024

With method #2, where it fails? at which index?

Perhaps your malloc() is broken (allocates from wrong memory). Try to copy to a static array.

 

sirpak
sirpakAuthor
Associate III
March 12, 2024

Still not working :thinking_face:..

I removed the free() at the end of the function, and it seemed to work.

It worked only once, the second time I test the code, it crashes. Maybe because the heap gets full?!

sirpak
sirpakAuthor
Associate III
March 14, 2024

Dear All,

thank you for your help.

I removed the malloc and I used an array of char with fixed size and all is working fine.

I use 4 kB of heap and 4 kB of stack.

Total SRAM memory is 64 kB.

Maybe I have some problem with memory allocation and it does not allow me to allocate it dynamically..