digitalmars.D.learn - Can't use toHexString
- badlink (6/6) Jul 19 2015 Hello,
- Kagamin (1/1) Jul 19 2015 http://forum.dlang.org/post/ydaeytbyxdnwlbpwkoma@forum.dlang.org
- badlink (7/8) Jul 19 2015 Argh... must use the search function first...
- Adam D. Ruppe (23/25) Jul 19 2015 The std.digest.toHexString and variants return a *static array*
- badlink (2/16) Jul 19 2015 Thank you very much for the explanation.
- Johannes Pfau (12/32) Jul 19 2015 Good catch. The version returning a string is meant to be used with the
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
Jul 19 2015
http://forum.dlang.org/post/ydaeytbyxdnwlbpwkoma forum.dlang.org
Jul 19 2015
On Sunday, 19 July 2015 at 12:00:23 UTC, Kagamin wrote:http://forum.dlang.org/post/ydaeytbyxdnwlbpwkoma forum.dlang.orgArgh... 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...
Jul 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...) 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.
Jul 19 2015
On Sunday, 19 July 2015 at 12:08:18 UTC, Adam D. Ruppe wrote:This line is illegal:Thank you very much for the explanation.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...) ...
Jul 19 2015
Am Sun, 19 Jul 2015 12:08:16 +0000 schrieb "Adam D. Ruppe" <destructionator gmail.com>:This line is illegal: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=9279return 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...)
Jul 19 2015