wcscpy broken
Ulrich Weigand
Ulrich.Weigand@de.ibm.com
Fri Jan 31 21:31:00 GMT 2003
Hello,
the implementation of wcscpy in glibc appears to be broken:
wchar_t *
wcscpy (dest, src)
wchar_t *dest;
const wchar_t *src;
{
wchar_t *wcp = (wchar_t *) src;
wint_t c;
const ptrdiff_t off = dest - src - 1;
do
{
c = *wcp++;
wcp[off] = c;
}
while (c != L'\0');
return dest;
}
Note the pointer difference 'dest - src'; this invokes undefined
behaviour according to the C standard because dest and src are
not guaranteed to point into the same array.
And in fact this generates incorrect code if one of dest and src
is not sizeof(wchar_t)-aligned (which, while unusual and a bit
inefficient, is valid as far as I can see).
I've had an actual bug report due to this because gcc 2.95.3 does
not always align wide character string constants to 4 bytes, so a
'wcscpy (str, L"some constant")' can trigger the bug.
Mit freundlichen Gruessen / Best Regards
Ulrich Weigand
--
Dr. Ulrich Weigand
Linux for S/390 Design & Development
IBM Deutschland Entwicklung GmbH, Schoenaicher Str. 220, 71032 Boeblingen
Phone: +49-7031/16-3727 --- Email: Ulrich.Weigand@de.ibm.com
More information about the Libc-alpha
mailing list