Can't use toHexString

Johannes Pfau via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 19 06:30:12 PDT 2015


Am Sun, 19 Jul 2015 12:08:16 +0000
schrieb "Adam D. Ruppe" <destructionator at 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




More information about the Digitalmars-d-learn mailing list