strncpy replacement in gcc arm

474 views Asked by At

Does strncpy() not have proper equivalent in arm which will take both destination size and number of source characters to be copied,

strlcpy(char * /*dst*/, const char * /*src*/, size_t /*len*/);

so here we have to just use strlcpy() and hope source string is null terminated?

MS has provided one which is perfect (at least appears to be ;)):

StringCchCopyN(LPTSTR pszDest, size_t cchDest, LPCTSTR pszSrc, size_t cchSrc);

To the uninitiated, strncpy() is not secure (why strncpy is unsafe]1).

2

There are 2 answers

0
0___________ On BEST ANSWER

You need to write your own ones

char *mystrncpy(char *dest, const char *src, size_t nbytes)
{
    char *svd = dest;

    if(nbytes)
    {
        dest[--nbytes] = 0;
        while(*src && nbytes-- )
             *dest++ = *src++;
    }
    return svd;
}

OR

char *mystrncpy(char *dest, const char *src, size_t destsize, size_t srcsize)
{
    memcpy(dest, src, destsize < srcsize ? destsize : srcsize);
    dest[destsize -1] = 0;
    return dest;
}
0
domen On

You could use snprintf(dest, size, "%s", source);

Note that source does need to be \0-terminated for snprintf, even if longer than size.