Hi Guys,
I wrote a mechanism (which I previously discussed here), which does the follow:
When Calling
funcA or
funcB, I set Stack Pointer to the first Address of
GlobalStackBuffer (that is a global array with enough space to handle local variables).
My bug is that I did not notice the following situation:
C:
void funcA(short int title, short int, size, short int *buff)
{
short int buff[size+1]; //Our compiler allows defining variable-size Array, which will be stored in the stack
//.... initializing buff
funcB(title, size+1, buff); //buff address is sent to funcB
//...
}
void funcB(short int title, short int, size, short int *buff)
{
//....
while (1);
}
Func A defines a local Array which since its a local array it's defined inside
GlobalStackBuffer.
Then
funcA calls
funcB and sends the array's address to
funcB as an argument----> As a result,
funcB will overrun the array's address, because the Stack of
funcB will start from the same
GlobalStackBuffer.
How would you solve this bug, while keeping the mechanism in which the Stack Pointer of
funcA and
funcB use
GlobalStackBuffer space?
An important thing to note is that there's no return from
funcB, since at the end of
funcB the program enters infinite loop.
I thought to solve it by just before
funcA calls
funcB, it'd store
buff's values inside the first address of
GlobalStackBuffer, and have SP of
funcB to start from
GlobalStackBuffer + sizeof(buff)
But you always have better solutions
Thank you very much.