Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #22877

Re: Function casting - UB?

Newsgroups comp.lang.c
From Heikki Kallasjoki <fis+usenet@zem.fi>
Subject Re: Function casting - UB?
References <0c176b1e-39f3-4e65-8a7b-eca16b8955c5@d24g2000yqh.googlegroups.com> <jsfdik$vtr$1@dont-email.me>
Organization nonexistent
Message-ID <slrnjumi6h.dba.fis@iris.zem.fi> (permalink)
Date 2012-06-27 17:54 +0000

Show all headers | View raw


On 2012-06-27, Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
> On 6/27/2012 11:50 AM, nroberts wrote:
>> In C++ this would be totally undefined.  How about in C?  It works on
>> my machine with my compiler...
...
>> typedef void (*fun)(int, int);
>>
>> void f(int i) {
>> 	int q = 33;
>> 	printf("%d %d\n", i, q);
>> }
>>
>> int main(void) {
>> 	fun funptr = (fun)f;
>> 	funptr(3,2);
>>
>> 	return 0;
>> }
...
> the 3 into.  But it would be a bad idea to generalize from the
> apparent success of this simple case to the notion "It works!"

I fully agree that empirical experimentation is not terribly useful
(and/or perhaps interesting), but FWIW, a counter-example:

$ cat tmp.c
#include <windows.h>

int WINAPI f(int a) { return a; }

int g(void) {
    int WINAPI (*p)(int, int) = (int WINAPI (*)(int, int))f;
    return p(0, 1);
}

int main(void) {
    return g();
}

$ i586-mingw32msvc-gcc -o tmp.exe tmp.c -O2 -fomit-frame-pointer
$ wine ./tmp.exe
wine: Unhandled page fault on read access to 0x00000001 at address 0x1
(thread 0022), starting debugger...

(For the "not really comp.lang.c material" details, see below.)

>      And then, there's wide variety in function linkage mechanisms.
> Sometimes the caller takes care of both setting up and disposing
> of the argument list, but sometimes the caller sets it up and the
> callee disposes -- and if the callee does cleanup for a one-argument
> list while the caller expected it to clean up two, it's blooey again.

The above case (involving the "WINAPI"/"stdcall" calling convention) is
an example of the latter, and with the frame pointer omitted, 'g' ends
up using the second provided parameter as its return address.

-- 
Heikki Kallasjoki

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Function casting - UB? nroberts <roberts.noah@gmail.com> - 2012-06-27 08:50 -0700
  Re: Function casting - UB? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-27 12:49 -0400
    Re: Function casting - UB? Heikki Kallasjoki <fis+usenet@zem.fi> - 2012-06-27 17:54 +0000
      Re: Function casting - UB? "io_x" <a@b.c.invalid> - 2012-06-28 18:58 +0200
  Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-27 10:37 -0700
    Re: Function casting - UB? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-28 09:23 +0200
      Re: Function casting - UB? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-28 09:27 +0200
        Re: Function casting - UB? Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-28 12:03 +0200
          Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-29 11:10 -0700
      Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-29 11:03 -0700
  Re: Function casting - UB? Les Cargill <lcargill99@comcast.com> - 2012-06-27 12:58 -0500
  Re: Function casting - UB? Les Cargill <lcargill99@comcast.com> - 2012-06-27 12:59 -0500
    Re: Function casting - UB? James Kuyper <jameskuyper@verizon.net> - 2012-06-27 14:45 -0400
      Re: Function casting - UB? "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-06-27 14:55 -0700
        Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-27 17:44 -0700
          Re: Function casting - UB? Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-07 16:46 -0700
            Re: Function casting - UB? Barry Schwarz <schwarzb@dqel.com> - 2012-07-08 09:19 -0700
              Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-08 11:13 -0700
                Re: Function casting - UB? Barry Schwarz <schwarzb@dqel.com> - 2012-07-08 15:24 -0700
                Re: Function casting - UB? Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-08 18:12 -0700
                Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-10 10:40 -0700
            Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-08 11:06 -0700

csiph-web