[Issue 15136] If we want toStringz to be fully correct, it needs to stop checking for '\0'

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Oct 30 13:48:02 UTC 2017


https://issues.dlang.org/show_bug.cgi?id=15136

Steven Schveighoffer <schveiguy at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy at yahoo.com

--- Comment #3 from Steven Schveighoffer <schveiguy at yahoo.com> ---
(In reply to John Colvin from comment #2)
> It seem to me like this could happen with static arrays directly on the
> stack as well, depending on what variables the compiler happens to put
> directly after.

It can be done with heap-allocated arrays as well:

auto s = "hello".idup;
auto str = s.toStringz;
assert(s.ptr == str);
s ~= 'b';
assert(s[5] == 'b');

The assumption that a coincidental null character after the string must mean it
is part of a literal is completely unsound. Not to mention, if the null happens
to be on a 4-byte boundary, then it doesn't even look at it, potentially
wasting an allocation.

What we really need, as Jonathan says, is a way to tell if a string pointer
points at a string literal or not.

This should be possible I would think, as the linker puts all the literals into
the same section, no? All we have to do is check whether the array points in
that section, and the null character pointer is in that section, and the null
character is null. Otherwise, allocate.

--


More information about the Digitalmars-d-bugs mailing list