Bug in wcsncpy

Martin Sjöstedt Martin.Sjostedt@ascom.se
Tue Aug 18 15:41:00 GMT 2009


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++;
  *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++;
  while (n) {
    *p++ = L'\0';

Are there any plans on changing this?

Best Regards,

Please consider the environment before printing this e-mail and any associated attachments    

More information about the Newlib mailing list