incorrect data when returning static array in place of dynamic

2015-07-05 Thread sigod via Digitalmars-d-learn
Consider this code: ``` import std.digest.digest; import std.stdio; ubyte[] hmac_sha1(const(ubyte)[] key, const(ubyte)[] message) { import std.digest.sha; enum block_size = 64; if (key.length > block_size) key = sha1Of(key); if (key.length < block

Re: incorrect data when returning static array in place of dynamic

2015-07-05 Thread thedeemon via Digitalmars-d-learn
On Sunday, 5 July 2015 at 18:57:46 UTC, sigod wrote: Why does function return incorrect data? Using `.dup` in return expression or using `ubyte[20]` as return type fixes problem, but why? Because sha1Of() returns ubyte[20], this is a stack-allocated array, a value type. If you put correct ret

Re: incorrect data when returning static array in place of dynamic

2015-07-06 Thread sigod via Digitalmars-d-learn
On Monday, 6 July 2015 at 05:30:46 UTC, thedeemon wrote: On Sunday, 5 July 2015 at 18:57:46 UTC, sigod wrote: Why does function return incorrect data? Using `.dup` in return expression or using `ubyte[20]` as return type fixes problem, but why? Because sha1Of() returns ubyte[20], this is a st

Re: incorrect data when returning static array in place of dynamic

2015-07-06 Thread anonymous via Digitalmars-d-learn
On Monday, 6 July 2015 at 07:48:17 UTC, sigod wrote: Aren't compiler smart enough to prevent it? ``` ubyte[] test1() { auto b = sha1Of(""); return b; // Error: escaping reference to local b } ubyte[] test2() { return sha1Of(""); // works, but returns incorrect data } ``

Re: incorrect data when returning static array in place of dynamic

2015-07-06 Thread via Digitalmars-d-learn
On Monday, 6 July 2015 at 10:20:28 UTC, anonymous wrote: dmd 2.068.0 catches this. You can get the beta here: http://downloads.dlang.org/pre-releases/2.x/2.068.0/ ... and it already contains a std.digest.hmac module :-)

Re: incorrect data when returning static array in place of dynamic

2015-07-10 Thread sigod via Digitalmars-d-learn
On Monday, 6 July 2015 at 10:20:28 UTC, anonymous wrote: On Monday, 6 July 2015 at 07:48:17 UTC, sigod wrote: Aren't compiler smart enough to prevent it? ``` ubyte[] test1() { auto b = sha1Of(""); return b; // Error: escaping reference to local b } ubyte[] test2() { re

Re: incorrect data when returning static array in place of dynamic

2015-07-10 Thread sigod via Digitalmars-d-learn
On Monday, 6 July 2015 at 14:56:38 UTC, Marc Schütz wrote: On Monday, 6 July 2015 at 10:20:28 UTC, anonymous wrote: dmd 2.068.0 catches this. You can get the beta here: http://downloads.dlang.org/pre-releases/2.x/2.068.0/ ... and it already contains a std.digest.hmac module :-) Yes, thanks.