Bug in wcsncpy

Howland Craig D (Craig) howland@LGSInnovations.com
Tue Aug 18 15:57:00 GMT 2009


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    
-------------- next part --------------
A non-text attachment was scrubbed...
Name: wcsncpy.patch
Type: application/octet-stream
Size: 3790 bytes
Desc: wcsncpy.patch
URL: <http://sourceware.org/pipermail/newlib/attachments/20090818/ecbe939c/attachment.obj>


More information about the Newlib mailing list