msvcrt: strncpy doesn't compliant C standard

Piotr Caban piotr.caban at gmail.com
Tue Aug 6 02:35:30 CDT 2013


On 08/06/13 09:09, Álvaro Nieto wrote:
>   char* __cdecl MSVCRT_strncpy(char *dst, const char *src, MSVCRT_size_t len)
>   {
> -    MSVCRT_size_t i;
> -
> -    for(i=0; i<len; i++)
> -        if((dst[i] = src[i]) == '\0') break;
> +    MSVCRT_size_t i, src_len;
> +    src_len = strlen(src);
> +
> +    for(i=0; i<src_len; i++)
> +        dst[i] = src[i];
> +
> +    /* C Standard */
> +    /* If the array pointed to by s2 is a string that is shorter than n bytes,
> +     * null bytes shall be appended to the copy in the array pointed to by s1,
> +     * until n bytes in all are written */
> +    for(i=src_len+1; i<len; i++)
> +        dst[i] = '\0';
>
>       return dst;
You can't call strlen here, src string doesn't have to be null 
terminated. Something like this will do the job:
for(i=0; i<len; i++)
     if((dst[i] = src[i]) == '\0') break;

for(; i<len; i++)
     dst[i] = 0;



More information about the wine-patches mailing list