Bug in wcsncpy

Jeff Johnston jjohnstn@redhat.com
Tue Aug 18 16:44:00 GMT 2009


Patch checked in.  Thanks Craig.

-- Jeff J.

Howland Craig D (Craig) wrote:
> Attached is a patch to correct the problem.  I adapted the code from
> the short version of strncpy(), which is very similar to the OpenBSD
> example that Martin quoted.  I also made some small tweaks to the
> documentation.
> 				Craig
>
>
> -----Original Message-----
> From: newlib-owner@sourceware.org [mailto:newlib-owner@sourceware.org] On Behalf Of Martin Sjöstedt
> Sent: Monday, August 17, 2009 8:13 AM
> To: newlib@sourceware.org
> Subject: Bug in wcsncpy
>
>
> Hi!
>
> We have discovered a problem with wcsncpy(s1, s2, n). The documentation states that the function shall copy no more than n wide characters from s2 into s1, and if less than n characters copied s1 shall be filled with zeroes up to n. 
>
> But the current implementation in newlib,
>
>   p = s1;
>   q = s2;
>   while (n && *q)
>   {
>     *p++ = *q++;
>     n--;
>   }
>   *p = '\0';
>
> , does copy n characters into s1 and then always puts one zero at the end (which might be s1[n+1]). 
>
> In a newer version of this file found in OpenBSD this is implemented like this:
>
>   p = s1;
>   while (n && *s2) {
>     *p++ = *s2++;
>     n--;
>   }
>   while (n) {
>     *p++ = L'\0';
>     n--;
>   }
>
> Are there any plans on changing this?
>
> Best Regards,
> Martin
>
> Please consider the environment before printing this e-mail and any associated attachments    
>   



More information about the Newlib mailing list