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 stack-allocated
array, a value type. If you put correct return type there, it
will be returned by value and everything's fine. If your return
type is ubyte[] (a reference type), a slice of stack-allocated
array is returned which creates a reference to stack data that
doesn't exist anymore.
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
}
```
Looks more like a bug to me.