[Issue 17258] New: Pass by name doesn't work reliably and can sometimes lead to memory corruption

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Mar 15 10:22:31 PDT 2017


https://issues.dlang.org/show_bug.cgi?id=17258

          Issue ID: 17258
           Summary: Pass by name doesn't work reliably and can sometimes
                    lead to memory corruption
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: petar.p.kirov at gmail.com

struct ByName1(alias Var)
{
    alias var = Var;
}

struct ByName2(alias Var)
{
    alias var = Var;

    void inc() { var++; }
}

void byName2(alias var)()
{
    var++;
}

void test1()
{
    int x;
    ByName1!x v;
    v.var++;
    assert(v.var == 1); // v.var is 1 => passes - OK
    assert(x == v.var); // x is 0 => fails  - NG
}

void test2()
{
    int x;
    ByName2!x v;
    v.inc();
    // import std.stdio;
    // writeln(v.var); // Prints garbage !!!
    assert(v.var == 1); // v.var is ??? => fails - NG
    assert(x == v.var); // x is 0, v.var is ??? => fails  - NG
}

void test3()
{
    int x;
    byName2!x();
    assert (x == 1); // x is 1 => passes - OK   
}

void main()
{
    test1(); // fails because &x != &v.var
    test2(); // fails because of bad code gen / memory corruption
    test3(); // passes
}

--


More information about the Digitalmars-d-bugs mailing list