www.digitalmars.com         C & C++   DMDScript  

D.gnu - [Issue 1965] New: std.string.toStringz checks too stringent

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1965

           Summary: std.string.toStringz checks too stringent
           Product: DGCC aka GDC
           Version: unspecified
          Platform: Macintosh
        OS/Version: All
            Status: NEW
          Severity: trivial
          Priority: P2
         Component: Phobos
        AssignedTo: dvdfrdmn users.sf.net
        ReportedBy: fawzi gmx.ch


If a string containing a zero character is passed to std.string.toStringz then
the checks fail.
Indeed the zero terminated string and the string are different, but I think
that the routine should be seen as working correctly.

(making the tests this actually happens as a string "\0" is passed to
toStringz).

I would replace the check with the one in the patch, but I can understand if
someone argues that the original check is a feature, not a bug.

Fawzi

============
--- string.d    (revision 209)
+++ string.d    (working copy)
   -219,7 +219,9   
     out (result)
     {
        if (result)
-       {   assert(strlen(result) == s.length);
+       {
+           auto clen=strlen(result);
+           assert(clen == s.length || (clen < s.length && s[clen]==0));
            assert(memcmp(result, s.ptr, s.length) == 0);
        }
     }
=================


-- 
Apr 01 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1965





------- Comment #1 from jason spashett.com  2008-04-17 10:39 -------
It's a feature one day and it's a nuisance the next.

Perhaps something LIKE this should be implemented:

char* toStringz(char[] s, bool noassert_on_embedded_nulls=false);


-- 
Apr 17 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1965





------- Comment #2 from fawzi gmx.ch  2008-04-17 11:01 -------
(In reply to comment #1)
 It's a feature one day and it's a nuisance the next.
 
 Perhaps something LIKE this should be implemented:
 
 char* toStringz(char[] s, bool noassert_on_embedded_nulls=false);

I LIKE this idea :) --
Apr 17 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1965


bugzilla digitalmars.com changed:

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




------- Comment #3 from bugzilla digitalmars.com  2008-04-23 01:24 -------
If strings with embedded zeros are passed to toStringz, then the function
should fail as the result makes no sense as a C 0-terminated string. The
function behaves correctly (although I will update the doc on this point).


-- 
Apr 22 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1965


braddr puremagic.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |




------- Comment #4 from braddr puremagic.com  2008-04-23 01:45 -------
This was fixed, though differently, in the upstream dmd phobos with these
submits:

1.x - http://www.dsource.org/projects/phobos/changeset/655
2.x - http://www.dsource.org/projects/phobos/changeset/561

I think the change I made was worth doing, but only for the 'the string already
contains a trailing \0' case.  I don't think stringz needs to bother with
handling arbitrary embedded nulls either.

I'm leaving the issue closed, since it's not typical that David needs a bug to
track each patch made to the upstream dmd/phobos code.  Presumably it'll be in
a future dgcc release as it's moved up to current dmd/phobos releases.


-- 
Apr 22 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1965





------- Comment #5 from braddr puremagic.com  2008-04-23 01:45 -------
This was fixed, though differently, in the upstream dmd phobos with these
submits:

1.x - http://www.dsource.org/projects/phobos/changeset/655
2.x - http://www.dsource.org/projects/phobos/changeset/561

I think the change I made was worth doing, but only for the 'the string already
contains a trailing \0' case.  I don't think stringz needs to bother with
handling arbitrary embedded nulls either.

I'm leaving the issue closed, since it's not typical that David needs a bug to
track each patch made to the upstream dmd/phobos code.  Presumably it'll be in
a future dgcc release as it's moved up to current dmd/phobos releases.


-- 
Apr 22 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1965


braddr puremagic.com changed:

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




-- 
Apr 22 2008