Casting char[] ranges to string can lead to unexpected behavior
Jesse Phillips via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Oct 30 20:36:01 PDT 2014
On Friday, 31 October 2014 at 02:04:00 UTC, Domingo wrote:
> I spent two days to find a nasty aleatory problem due to a
> string been assigned a range from a char[] array.
>
> This issue on vibe.d detail it a bit more:
> https://github.com/rejectedsoftware/vibe.d/issues/889
>
> I'm putting it here to call attention for other developers for
> this problem.
>
> Cheers !
To recap, the issue is that a char[] buffer is being overridden
while a immutable reference was created to the same data (string).
Essentially you can't cast a char[] to string unless it is the
only reference that will remain. Even so, I wouldn't recommend
cast in those cases and instead use std.exception.assumeUnique to
better state the assumption made.
http://dlang.org/phobos/std_exception.html#.assumeUnique
If the mutable reference will remain the duplicating the char[]
is necessary and the reason for .idup.
More information about the Digitalmars-d-learn
mailing list