Why does casting a function generate a run-time abort?

Mark Mitchell mark@codesourcery.com
Fri Jun 18 02:07:00 GMT 2004


Ziemowit Laski wrote:

> Compiling the following snippet
> 
>   typedef struct objc_Object * foo_id ;
>   typedef struct blortstruct *blort ;
>   extern foo_id objc_msgSend(foo_id, ...);
> 
>   int main(int argc, char * argv[])
>   {
>     blort foo = 0, bar = 0 ;
>     foo_id obj0;
>     obj0 = ((foo_id ((*)(foo_id, char *, blort, blort))) &objc_msgSend) 
> (obj0, "foo message", foo, bar);
>     return 0;
>   }
> 
> on the current TOT produces the following messages:
> 
>   R3692996.c: In function `main':
>   R3692996.c:9: warning: function called through a non-compatible type
>   R3692996.c:9: note: if this code is reached, the program will abort
> 
> Could someone shed some light on this?

In general, this code is unsafe.

It's assuming that the calling conventions for a varargs function and a 
non-varargs function are the same, and that's not necessarily true on 
all platforms.

I'm not sure why we're transforming that into a call to "abort", rather 
than just crossing our fingers and hoping things will work out, though. 
  I think we should be issuing the warning, but generating code as if 
the function had the type to which the pointer points.

-- 
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com



More information about the Gcc mailing list