Thread overview
[Issue 15136] If we want toStringz to be fully correct, it needs to stop checking for '\0'
Oct 02, 2015
Andrej Mitrovic
Oct 03, 2015
John Colvin
Oct 28, 2017
Shriramana Sharma
Feb 20, 2021
Dlang Bot
Feb 22, 2021
Dlang Bot
October 02, 2015
https://issues.dlang.org/show_bug.cgi?id=15136

Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com

--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> ---
+1. If we really need to avoid memory allocations let's just add an overload or something like assumeZeroTerminated or something..

--
October 03, 2015
https://issues.dlang.org/show_bug.cgi?id=15136

John Colvin <john.loughran.colvin@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |john.loughran.colvin@gmail.
                   |                            |com

--- Comment #2 from John Colvin <john.loughran.colvin@gmail.com> ---
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.

--
October 28, 2017
https://issues.dlang.org/show_bug.cgi?id=15136

Shriramana Sharma <samjnaa@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |samjnaa@gmail.com

--
October 30, 2017
https://issues.dlang.org/show_bug.cgi?id=15136

Steven Schveighoffer <schveiguy@yahoo.com> changed:

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

--- Comment #3 from Steven Schveighoffer <schveiguy@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.

--
October 30, 2017
https://issues.dlang.org/show_bug.cgi?id=15136

--- Comment #4 from Steven Schveighoffer <schveiguy@yahoo.com> ---
(In reply to Steven Schveighoffer from comment #3)
> assert(s[5] == 'b');

That should have read str[5], obviously :)

--
February 20, 2021
https://issues.dlang.org/show_bug.cgi?id=15136

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

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

--- Comment #5 from Dlang Bot <dlang-bot@dlang.rocks> ---
@aG0aep6G created dlang/phobos pull request #7806 "fix issue 15136 - If we want toStringz to be fully correct, it needs …" fixing this issue:

- fix issue 15136 - If we want toStringz to be fully correct, it needs to stop checking for '\0'

https://github.com/dlang/phobos/pull/7806

--
February 22, 2021
https://issues.dlang.org/show_bug.cgi?id=15136

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

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

--- Comment #6 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/phobos pull request #7806 "fix issue 15136 - If we want toStringz to be fully correct, it needs …" was merged into master:

- c55ce44705ddac208d5d7340b60ad2f8eacd0552 by aG0aep6G:
  fix issue 15136 - If we want toStringz to be fully correct, it needs to stop
checking for '\0'

https://github.com/dlang/phobos/pull/7806

--