Skip to contentSkip to conversation

bad function ptrs - is it dangerous ?

DISCUSSION Mikael Pettersson 1 participant 1 message Oct 2, 2002

lore
Share & tools
lore
#0 lore raw
Quoted 8 lines · J.A. Magallon

int (*pf)(data *);
int f(data*);

so you can:

pf = f;
pf(data).

Quoted 7 lines

Fine. But what happens if:

void (*pf)(data *);
int f(data*);

pf = f; // gcc happily swallows, gcc-3.2 gives a warning. pf(data).

Undefined Behaviour. I can easily imagine cases where, depending on the calling convention and the actual return type, things could go very very wrong. Consider struct returns...

This case, returning an int to a caller expecting void, is likely to work on most normal machines -- the int would go into a GP result register, and the GP result register is typically always part of the caller-save set. The code is still utter crap, however.

Quoted 2 lines

The (in)famous graphics driver all you know is doing this with the copy_info op for gart...

<censored>

/Mikael