Modify Function Pointer to Take Additional Parameters

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 19 19:18:42 PST 2016


On Friday, 19 February 2016 at 22:34:48 UTC, Chris Wright wrote:
>
> I tested this a fair bit today, and I haven't been able to do 
> any of the nefarious things I expected to be able to do. No 
> overwriting variables in the caller's scope, no smashing stack 
> pointers, etc.
>
> I was surprised by this result, but in retrospect, it's 
> relatively obvious. The caller pushes variables onto the stack 
> and sets the stack pointer for the callee. It wouldn't send a 
> stack pointer that pointed into its own stack frame.

Thanks for taking the time to test.

The more I've thought about it, the more I wonder if there should 
be a restriction so that casts of function pointers/delegate 
maintain the same number of parameters. Even though you haven't 
been able to do nefarious things, it's giving a completely wrong 
answer than you would expect. The result of the answer might 
cause bad things to happen in a program. Further, to even 
understand what's going wrong you have to understand how the 
compiler is generating assembly. I've been using D for like a 
year or so, and I would never have been able to figure out the 
reason by myself.

Or at least in safe code you shouldn't be able to do this.


More information about the Digitalmars-d-learn mailing list