Page 1 of 1

Possible problem in helpfile with the "strncpy" function

Posted: 05 May 2015 17:29
by Dany
This is valid for all mB compilers, not only that for PIC:

In the compiler's help for the "strncpy" function (String library), the "Dst" var parameter is described as a "string" type.
A string should always be terminated by a zero byte, but the function does not always add it, so in certain cases the "Dst" is no proper string because it is unterminated.

This can lead to memory corruption.

This issue was detected here: http://www.mikroe.com/forum/viewtopic.php?f=81&t=64329.
The ansi C definition of StrnCpy is indeed so that the terminating zero is not always added.

Two possibilities exist to remove the inconsistency:
- change the StrnCpy function so that it always add a trailing zero (which makes it different from the "standard" strncpy defintion), or
- Adapt the type of the "Dst" parameter in the helpfile (make it an array of bytes)...

In Janni's replacement libraries (http://www.cobir.com/replibs.htm#Strings) he chose the first option... :D :D