Code security: "auto" / Reason for errors

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Wed Jun 1 14:11:31 PDT 2016


On 01.06.2016 17:34, deadalnix wrote:
> On Wednesday, 2 March 2016 at 19:42:02 UTC, Ozan wrote:
>> Hi
>>
>> I despair of "auto var1 = var2"for arrays. Isn't it a open door for
>> errors. Example
>>
>> import std.stdio;
>>
>> void main()
>> {
>>     int[] a;
>>     foreach(i; 0..10) a ~= i;
>>     auto b = a; // correct dlang coding: auto b = a.dup;
>>
>>     a[2] = 1;
>>     b[2] = 5; // Overwrites assignment before
>>     writeln(a);
>>     writeln(b); // Always a == b but developer would like to have (a
>> != b)
>> }
>>
>> The behaviour is different to other non-container datatypes.
>> So in a first view, it looks like a data copy but it's only a pointer
>> copy.
>>
>> Regards, Ozan
>
> Everything behaves as designed, auto changes nothing in the example and
> there is no security concern.
>
> We have a bingo.
>

Mutable aliasing can be error prone if it is not what you need, because 
then it is essentially a form of manual memory management. Built-in 
slices are likely just too low-level for the OP.


More information about the Digitalmars-d mailing list