Thread overview
[Issue 21038] wchar and dchar string alignment should be 2 and 4, respectively
[Issue 21038] Wrong codegen when calling wcslen
Jul 11, 2020
ag0aep6g
Aug 07, 2020
Walter Bright
Aug 07, 2020
Walter Bright
Aug 07, 2020
Walter Bright
Aug 07, 2020
Dlang Bot
Aug 13, 2020
Dlang Bot
July 11, 2020
https://issues.dlang.org/show_bug.cgi?id=21038

ag0aep6g <ag0aep6g@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
                 CC|                            |ag0aep6g@gmail.com

--- Comment #1 from ag0aep6g <ag0aep6g@gmail.com> ---
The string data is getting misaligned. wcslen assumes properly aligned data. testabcd.d can be reduced to this:

----
alias wchar_t = dchar;
const(wchar_t)* name = "abcd";
void test()
{
    assert((cast(size_t) name) % wchar_t.sizeof == 0); /* Fails. Should pass.
*/
}
----

--
August 07, 2020
https://issues.dlang.org/show_bug.cgi?id=21038

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com

--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> ---
For the program:

alias wchar_t = dchar;
const(wchar_t)* x = "xz";
const(wchar_t)* name = "abcd";
void test()
{
    assert((cast(size_t) name) % wchar_t.sizeof == 0); /* Fails. Should pass.
*/
}

the output generated is:

Section 6  .rodata  PROGBITS,ALLOC,SIZE=0x0030(48),OFFSET=0x0040,ALIGN=16
 0040:  78  0  0  0 7a  0  0  0  0  0  0  0 61  0  0  0   x...z.......a...
 0050:  62  0  0  0 63  0  0  0 64  0  0  0  0  0  0  0   b...c...d.......
 0060:   4 10  0  0  0  0  0  0 74 65 73 74  0  0  0  0   ........test....

It's a surprise to me that a 4 byte element array is supposed to be aligned to 8 bytes. I'm not seeing where this is a requirement?

--
August 07, 2020
https://issues.dlang.org/show_bug.cgi?id=21038

--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> ---
Oh, I see now. For:

alias wchar_t = dchar;
const(wchar)* x = "xz";
const(wchar_t)* name = "abcd";
void test()
{
    assert((cast(size_t) name) % wchar_t.sizeof == 0); /* Fails. Should pass.
*/
}

the result is:

Section 6  .rodata  PROGBITS,ALLOC,SIZE=0x0030(48),OFFSET=0x0040,ALIGN=16
 0040:  78  0 7a  0  0  0 61  0  0  0 62  0  0  0 63  0   x.z...a...b...c.
 0050:   0  0 64  0  0  0  0  0  0  0  0  0  0  0  0  0   ..d.............
 0060:   4 10  0  0  0  0  0  0 74 65 73 74  0  0  0  0   ........test....

which is wrongly aligned on a 2 byte boundary.

--
August 07, 2020
https://issues.dlang.org/show_bug.cgi?id=21038

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Wrong codegen when calling  |wchar and dchar string
                   |wcslen                      |alignment should be 2 and
                   |                            |4, respectively

--
August 07, 2020
https://issues.dlang.org/show_bug.cgi?id=21038

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #4 from Dlang Bot <dlang-bot@dlang.rocks> ---
@WalterBright created dlang/dmd pull request #11528 "fix Issue 21038 - wchar and dchar string alignment should be 2 and 4,…" fixing this issue:

- fix Issue 21038 - wchar and dchar string alignment should be 2 and 4, respectively

https://github.com/dlang/dmd/pull/11528

--
August 13, 2020
https://issues.dlang.org/show_bug.cgi?id=21038

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #5 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dmd pull request #11528 "fix Issue 21038 - wchar and dchar string alignment should be 2 and 4,…" was merged into master:

- 46994f578813b365050ce19ed0e0bcc132e7555b by Walter Bright:
  fix Issue 21038 - wchar and dchar string alignment should be 2 and 4,
respectively

https://github.com/dlang/dmd/pull/11528

--