cancel
Showing results for 
Search instead for 
Did you mean: 

Hard fault caused by strncpy,

conrad c
Associate II

Hi,

I am running into a strange problem, I am using strncpy function to copy one array into another.

If i include this in my code i get a Hard fault DEBUG_VT . I have read quite few article on hard faults and how to track them down but this one that is not covered . According to the documents this is reserved for debug purposes . The probelm is i am not even in debug mode when the code is running .

I am using Attolic IDE 9.3.0 , STM32F302CC

Does some one have some help to offer me

3 REPLIES 3
Danish1
Lead II

My guess is that you've fallen foul of a "gotcha" in strncpy.

If the string you're trying to copy is the same length (or longer) than the n you pass to strncpy, the destination string will NOT be terminated by a '\0' character. So you then can't use it as a regular C string (e.g. to pass to printf or strcpy).

Some systems have an alternative to strncpy which is strlcpy; that will only copy up to n-1 characters but will always put a terminating '\0' to mark the end of the string.

Hope this helps,

Danish

Check whether the pointers you pass to strncpy() both point into areas of memory which are actually a memory, i.e. RAM or FLASH.

JW

conrad c
Associate II

Hi ,

thanks for all the input, its appreciated.

I managed to track down the fault , both the suggestions had a key to the problem.

As the document says Hard faults usually is a exceptinon not being handled, although this is true its very much dependent on the type of hard fault.

If all interupts priorities are set and cleared correctly the only hard faults the could exist is somewhere to do with code.

in my case i used STNCPY to copy two arrays , the problem is that on closer inspection the array being copied from is filled with NULL chars . The STRNCPY function did not like this and caused a hard fault DEBUG_VT .

hope it help some other guys

regards

Conrad