Jump to page: 1 2 3
Thread overview
[Issue 3797] New: Function pointers need to be stricter
Feb 12, 2010
yebblies
Oct 18, 2010
Sobirari Muhomori
Oct 18, 2010
Sobirari Muhomori
[Issue 3797] Implicit conversion between incompatible function pointers
Oct 18, 2010
Sobirari Muhomori
Oct 18, 2010
Sobirari Muhomori
Oct 19, 2010
yebblies
Oct 19, 2010
Sobirari Muhomori
Oct 19, 2010
Sobirari Muhomori
May 09, 2011
yebblies
May 13, 2011
yebblies
May 13, 2011
yebblies
[Issue 3797] Regression(2.038): Implicit conversion between incompatible function pointers
May 14, 2011
timon.gehr@gmx.ch
Jun 05, 2011
yebblies
Jun 07, 2011
yebblies
Jun 07, 2011
yebblies
Jun 07, 2011
yebblies
Jun 07, 2011
klickverbot
Jun 08, 2011
yebblies
Jun 09, 2011
yebblies
Sep 02, 2011
Walter Bright
February 12, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3797

           Summary: Function pointers need to be stricter
           Product: D
           Version: 2.040
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: yebblies@gmail.com


--- Comment #0 from yebblies <yebblies@gmail.com> 2010-02-12 05:34:08 PST ---
Currently functions pointers can be implicitly converted despite having
different calling conventions.  If at all possible, function pointers should
require a cast to change calling convention.
In a related problem, it is possible to implicitly convert function pointers
with different argument lists.

The following code compiles but segfaults (Access Violation) on dmd 2.040 /
winxp.

import std.stdio;

void foo(real f)
{
    writeln("bar ", f);
}

void main()
{
    void function(ubyte[4]) ptr1 = &foo;
    extern(C) void function(long, long) ptr2 = ptr1;

    ptr1([0xFF, 0xFF, 0xFF, 0xFF]);
    ptr2(3, 8);
}

Function pointers cannot, however, be cast to a pointer with a different return
type.
To solve this problem: Disable all implicit conversions between function
pointers, requiring function parameters, return type, and calling convention to
match for assignment copying.  This prevents accidental conversions and allows
template code to be aware of the correct calling convention.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3797


Sobirari Muhomori <dfj1esp02@sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #1 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-10-18 10:58:09 PDT ---
*** Issue 5069 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3797


Sobirari Muhomori <dfj1esp02@sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid
           Severity|enhancement                 |major


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3797



--- Comment #2 from bearophile_hugs@eml.cc 2010-10-18 11:06:24 PDT ---
Sobirari Muhomori has closed bug 5069 because it's a duplicate of this one. But please don't ignore one thing I've written in bug 5069, about error messages, where for a wrong usage of std.c.stdlib.qsort I have asked for a single better error message (instead of the current two worse error messages):


Line 15: Error: cannot implicitly convert expression (& compare) of type int
function(const void*, const void*) to extern(C) int function(const void*, const
void*)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3797


Sobirari Muhomori <dfj1esp02@sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|major                       |critical


--- Comment #3 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-10-18 11:09:27 PDT ---
Uh, crash has a critical severity.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3797



--- Comment #4 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-10-18 11:14:48 PDT ---
> The following code compiles but segfaults (Access Violation) on dmd 2.040 /
> winxp.

Is this a cross-platform bug?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3797



--- Comment #5 from yebblies <yebblies@gmail.com> 2010-10-18 19:29:40 PDT ---
(In reply to comment #4)
> Is this a cross-platform bug?

Yes.  The problem is that pointers to functions with different calling conventions and parameters can be implicitly converted to each other.  This can easily cause stack corruption and/or corrupted parameters.  This is not unique to any platform.

------------------------------

import std.stdio;
import std.typetuple;

extern(D)
{
    void d1() { writeln("d1"); }
    void d2(int a) { writeln("d2(", a, ")"); }
    void d3(int a, int b) { writeln("d3(", a, ", ", b, ")"); }
    void d4(long a, long b) { writeln("d4(", a, ", ", b, ")"); }
}
extern(C)
{
    void c1() { writeln("c1"); }
    void c2(int a) { writeln("c2(", a, ")"); }
    void c3(int a, int b) { writeln("c3(", a, ", ", b, ")"); }
    void c4(long a, long b) { writeln("c4(", a, ", ", b, ")"); }
}
extern(Windows)
{
    void w1() { writeln("w1"); }
    void w2(int a) { writeln("w2(", a, ")"); }
    void w3(int a, int b) { writeln("w3(", a, ", ", b, ")"); }
    void w4(long a, long b) { writeln("w4(", a, ", ", b, ")"); }
}
extern(C++)
{
    void cpp1() { writeln("cpp1"); }
    void cpp2(int a) { writeln("cpp2(", a, ")"); }
    void cpp3(int a, int b) { writeln("cpp3(", a, ", ", b, ")"); }
    void cpp4(long a, long b) { writeln("cpp4(", a, ", ", b, ")"); }
}
extern(Pascal)
{
    void p1() { writeln("p1"); }
    void p2(int a) { writeln("p2(", a, ")"); }
    void p3(int a, int b) { writeln("p3(", a, ", ", b, ")"); }
    void p4(long a, long b) { writeln("p4(", a, ", ", b, ")"); }
}

void main()
{
    size_t stack;

    asm { mov stack, ESP; }
    writeln(stack);

    alias TypeTuple!(d1, d2, d3, d4, c1, c2, c3, c4, w1, w2, w3, w4, cpp1,
cpp2, cpp3, cpp4, p1, p2, p3, p4) functions;

    auto fptr = &d3;

    foreach(f; functions)
    {
        fptr = &f;
        fptr(1, 2);
    }

    asm { mov stack, ESP; }
    writeln(stack);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3797


Sobirari Muhomori <dfj1esp02@sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #6 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-10-19 11:03:16 PDT ---
*** Issue 4893 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3797


Sobirari Muhomori <dfj1esp02@sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |2573


--- Comment #7 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-10-19 11:04:48 PDT ---
also
---
import core.stdc.stdio;

void f(int[] a)
{
    a[0]=42;
}

int main()
{
    immutable int[] b = [1,2,3];
    void function(const int[] a) g=&f;
    printf("%d\n",b[0]);
    g(b);
    printf("%d\n",b[0]);
    return 0;
}
---
1
42

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 09, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3797


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr@gmx.ch


--- Comment #8 from yebblies <yebblies@gmail.com> 2011-05-09 04:43:55 PDT ---
*** Issue 5827 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2 3