www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20110] New: Module constructor implicitly converts a delegate

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

          Issue ID: 20110
           Summary: Module constructor implicitly converts a delegate
                    pointer into a function pointer
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: andrej.mitrovich gmail.com

-----
import std.stdio;

alias void function(int, int) Callback;

void passCallback ( Callback cb )
{
    cb(10, 20);
}

class C
{
    static this()
    {
        passCallback(&test);  // doesn't fail????
    }

    void test (int foo, int bar)
    {
        writefln("foo: %s, bar: %s", foo, bar);
    }
}

void main ()
{
    auto c = new C;
    static assert(!is(typeof(passCallback(&c.test))));  // correct
}
import std.stdio;

alias void function(int, int) Callback;

void passCallback ( Callback cb )
{
    cb(10, 20);
}

class C
{
    static this()
    {
        passCallback(&test);  // doesn't fail????
    }

    void test (int foo, int bar)
    {
        writefln("foo: %s, bar: %s", foo, bar);  // corrupt parameter
    }
}

void main ()
{
    auto c = new C;
    static assert(!is(typeof(passCallback(&c.test))));  // correct
}
-----

I don't know if this is an edge-case in the implementation, but it's really
dangerous.

In my case, I tried to pass a pointer to an extern(C) function to a C library,
and got corruption. It's because I forgot to mark my function as `static`, so I
was actually passing a function which has the hidden 'this' object as the first
parameter..

--
Aug 05 2019