A weird example of .toUTF16z concatination side-effects in wcsncat
Stanislav Blinov
stanislav.blinov at gmail.com
Thu Apr 7 12:51:26 UTC 2022
On Thursday, 7 April 2022 at 10:50:35 UTC, BoQsc wrote:
> wchar_t* clang_string = cast(wchar_t *)"AAAAAAAAAA";
You're witnessing undefined behavior. "AAAAAAAAAA" is a string
literal and is stored in the data segment. Mere cast to wchar_t*
does not make writing through that pointer legal. Moreover, even
if it was legal to write through it, that alone wouldn't be
sufficient. From documentation of `wcsncat`:
> The behavior is undefined if the destination array is not large
> enough for the contents of both str and dest and the
> terminating null wide character.
`wcsncat` does not allocate memory, it expects you to provide a
sufficiently large mutable buffer. For example, like this:
```d
// ...
auto cls = new wchar_t[256];
cls[] = 0;
cls[0..10] = 'A';
wchar_t* clang_string = cls.ptr;
// ...
```
More information about the Digitalmars-d-learn
mailing list