This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
Re: wcscpy broken
- From: James Antill <nevyn-glibc-alpha at and dot org>
- To: "Ulrich Weigand" <Ulrich dot Weigand at de dot ibm dot com>
- Cc: libc-alpha at sources dot redhat dot com
- Date: 31 Jan 2003 17:20:34 -0500
- Subject: Re: wcscpy broken
- References: <OFDE846387.E2CBDD47-ONC1256CBF.0064A5D1@de.ibm.com>
"Ulrich Weigand" <Ulrich.Weigand@de.ibm.com> writes:
> 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.
But is valid code for gcc, glibc uses a lot of idioms that aren' ISO
9899 blessed.
> 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).
ISO 9899:1999 says...
The wcscpy function copies the wide string pointed to by s2
(including the terminating null wide character) into the array
pointed to by s1.
...if it's not aligned properly, then it's not a valid wide
string.
> 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.
That's a compiler bug. And you'll get failures for: wcslen(),
wcsnlen(), wprintf(), etc. and all code outside glibc which does
similar things.
--
# James Antill -- james@and.org
:0:
* ^From: .*james@and\.org
/dev/null