Bug 1965 – std.string.toStringz checks too stringent

Status
RESOLVED
Resolution
FIXED
Severity
trivial
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
PowerPC
OS
All
Creation time
2008-04-01T07:47:00Z
Last change time
2015-06-09T05:14:50Z
Assigned to
dvdfrdmn
Creator
fawzi

Comments

Comment #0 by fawzi — 2008-04-01T07:47:21Z
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); } } =================
Comment #1 by jason — 2008-04-17T10:39:52Z
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);
Comment #2 by fawzi — 2008-04-17T11:01:14Z
(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 :)
Comment #3 by bugzilla — 2008-04-23T01:24:16Z
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).
Comment #4 by braddr — 2008-04-23T01:45:17Z
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.
Comment #5 by braddr — 2008-04-23T01:45:33Z
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.