Curious strncpy() warning

I got this warning:

For this line of code:

strncpy(Transfer_Parameters.File_Info.File_Name, &Transfer_Parameters.Transfer_Packet[2], sizeof(Transfer_Parameters.File_Info.File_Name));

I can easily make the compiler happy with:

size_t Shhhhhhhh_PlatformIO = sizeof(Transfer_Parameters.File_Info.File_Name);
strncpy(Transfer_Parameters.File_Info.File_Name, &Transfer_Parameters.Transfer_Packet[2], Shhhhhhhh_PlatformIO);

I am just curious… Is my first strncpy() call not good practice?

You use Transfer_Parameters.File_Info.File_Name like a pointer so you calculate sizeof the pointer, not the actual string like you probably intended to. This can cause dangerous bugs so the compiler warns you.

2 Likes

Holy smokes…

I forgot that I changed the Transfer_Parameters.File_Info.File_Name from a character array to a character pointer at the end of the day yesterday.

This was my first compile since that.

Thanks @Thomseeen and PlatformIO compiler warnings.