Thread overview
Can't use toHexString
Jul 19, 2015
badlink
Jul 19, 2015
Kagamin
Jul 19, 2015
badlink
Jul 19, 2015
Adam D. Ruppe
Jul 19, 2015
badlink
Jul 19, 2015
Johannes Pfau
July 19, 2015
Hello,
I get different results if I either use toHexString or crcHexString.
I can't explain this behavior because crcHexString is just an alias of toHexString...

Test code: http://pastebin.com/33Ye7yyJ
July 19, 2015
http://forum.dlang.org/post/ydaeytbyxdnwlbpwkoma@forum.dlang.org
July 19, 2015
This line is illegal:

> return toHexString!(Order.decreasing)(crc.finish());

The std.digest.toHexString and variants return a *static array* which is static data and has a scope lifetime.

It is horrendous a compiler bug that allows it to implicitly convert to string - it should not compile because it silently gives bad results, escaping a reference to temporary memory while claiming it is immutable.

Confusingly though, this line is fine:

> return crcHexString(crc.finish());

It is an EXTREMELY subtle difference in the function signature (and totally bug prone, yet the documentation doesn't call it out...)

http://dlang.org/phobos/std_digest_digest.html#.toHexString


Notice the first overload there returns a static array, char[num*2], two of them return `auto`, so who knows what the hell they do.... and one actually returns `string`.

Slightly different arguments will give you wildly different results.



The safest thing to do is to `import std.conv;` and always `to!string` any of those *HexString return values if you want to return them like this.

Or you could also keep them in a local variable and use them before the function returns, then the stack allocated static array is OK too.
July 19, 2015
On Sunday, 19 July 2015 at 12:00:23 UTC, Kagamin wrote:
> http://forum.dlang.org/post/ydaeytbyxdnwlbpwkoma@forum.dlang.org

Argh... must use the search function first...

If I understand correctly in my case toHexString is returning a static char[len].
But why it gets silently converted to an immutable(char)[] ?
And why crcHexString works fine ? It's an alias to the same function...
July 19, 2015
On Sunday, 19 July 2015 at 12:08:18 UTC, Adam D. Ruppe wrote:
> This line is illegal:
>
>> return toHexString!(Order.decreasing)(crc.finish());
>
> The std.digest.toHexString and variants return a *static array* which is static data and has a scope lifetime.
>
> It is horrendous a compiler bug that allows it to implicitly convert to string - it should not compile because it silently gives bad results, escaping a reference to temporary memory while claiming it is immutable.
>
> Confusingly though, this line is fine:
>
>> return crcHexString(crc.finish());
>
> It is an EXTREMELY subtle difference in the function signature (and totally bug prone, yet the documentation doesn't call it out...)
> ...

Thank you very much for the explanation.
July 19, 2015
Am Sun, 19 Jul 2015 12:08:16 +0000
schrieb "Adam D. Ruppe" <destructionator@gmail.com>:

> This line is illegal:
> 
> > return toHexString!(Order.decreasing)(crc.finish());
> 
> The std.digest.toHexString and variants return a *static array* which is static data and has a scope lifetime.
> 
> It is horrendous a compiler bug that allows it to implicitly convert to string - it should not compile because it silently gives bad results, escaping a reference to temporary memory while claiming it is immutable.
> 
> Confusingly though, this line is fine:
> 
> > return crcHexString(crc.finish());
> 
> It is an EXTREMELY subtle difference in the function signature (and totally bug prone, yet the documentation doesn't call it out...)
> 

Good catch. The version returning a string is meant to be used with the interface digest API which produces ubyte[]. As we don't know the length at compile time in that case we can't return a static array. But in hindsight it might have been better to use another function for this and not an overload, especially considering the static array=>string conversion bug.

Documentation pull request: https://github.com/D-Programming-Language/phobos/pull/3500

The good news is that the coversion bug has finally been fixed: https://issues.dlang.org/show_bug.cgi?id=9279