Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Solving a bug

Status
Not open for further replies.

electroRF

Member
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.
 
Last edited:
Stack is usually used to store local variables. When a function is called, a return address (and someties parameters) are pushed into the stack. Then the function reserves the space in the stack. With next function is called, it doesn't resuse the stack space but rather continues to fill it in. So, in function B the stack will look like:

Parameters for funcA
Return address for funcA
Local variables for funcA (which includes the whole "buff" array)
Parameters for funcB (which includes pointer to "buff")
Return address for funcB
Local variables for funcB

It is therefore perfectly perfect to use buff in funcB. It'll not overwrite anything (unless you go over bounds).

Some compilers will also use frame pointers, but this doesn't change anything.
 
Hi NorthGuy,

You're right.

However, due to my mechanism, when calling funcB from anywhere, they arrive funcB_envelope, which all it does is set Stack Pointer to the first address of GlobalStackBuffer, and then call funcB.

Same operation was done when calling funcA from anywhere, they'll arrive funcA_envelope, which will set Stack Pointer to the first address of GlobalStackBuffer, and then call funcA.

Therefore, funcB will override buff which was defined in funcA.
 
However, due to my mechanism, when calling funcB from anywhere, they arrive funcB_envelope, which all it does is set Stack Pointer to the first address of GlobalStackBuffer, and then call funcB.

Same operation was done when calling funcA from anywhere, they'll arrive funcA_envelope, which will set Stack Pointer to the first address of GlobalStackBuffer, and then call funcA.

Therefore, funcB will override buff which was defined in funcA.

I would suggest abandoning this mechanism. If you want to switch stacks, each stack should have its own pointer.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top