Stack overflow in C function call - MS Visual C ++ 2010 Express

I wrote a function in C that, when called, immediately leads to a stack overflow.

Prototype: void dumpOutput( Settings *, char **, FILE * );

Calling line: dumpOutput( stSettings, sInput, fpOut );

At the time of its call, stSettings already a pointer to the Settings structure, sInput is a dynamically distributed 2D array, and fpOut is FILE * . It reaches the calling line without any errors, memory leaks, etc.

The actual function is quite long, and I think it’s not worth sharing it here, because the overflow occurs just like the code enters the function (the so-called part of the prolog, I think)

I tried calling the same function directly from main() with dummy variables to check if there are any problems with the arguments passed, but it still throws a condition.

The error occurs from chkstk.asm when the function is called. This asm file (in accordance with the comments presented in it) tries to check the stack to check / allocate memory for the called function. It just keeps jumping to the Find next lower page and probe until.

Local variables in dumpOutput also not memory characters, only 6 integers and 2 pointers.

The memory used by the code at the time of entering this function is 60.936K, which increases to 61.940K at the point when the stack overflows. Most of this memory is in sInput . Is this the cause of the error? I don’t think so, because only its pointer is passed. Secondly, I do not understand why dumpOutput trying to allocate 1004K of memory on the stack?

I do not understand here at all. Any help would be greatly appreciated.

Thanks in advance.

+4
source share
2 answers

By design, the task of _chkstk () is to create an exception. You can diagnose it by looking at the generated machine code. After you enter this function, right-click on the editing window and select "Go to disassembly". You should see something similar to this:

 003013B0 push ebp 003013B1 mov ebp,esp 003013B3 mov eax,1000D4h ; <== here 003013B8 call @ILT+70(__chkstk) (30104Bh) 

The value passed through the EAX register is important so that the amount of stack space matches your function. Chkstk then verifies that it is indeed accessible by examining the pages of the stack. If you see that it repeats cyclically, the EAX value in the code is very large. Like mine, it is guaranteed that it will consume all bytes of the stack. And more. This is what it defends against; you usually get an access violation exception. But there is no guarantee that your code may accidentally be written to a linked page that belongs to, say, a heap. This would create an incredibly complex error diagnosis. Chkstk () helps you find these errors before you blow your brains upset.

I just did it with this little test function:

 void test() { char kaboom[1024*1024]; } 

We cannot see yours, but the exception says that you have a large array as a local variable or you are passing a large _alloca () value. Correct by selecting this array from the heap.

+4
source

Most likely a stack error or a recursion error, but it's hard to answer without seeing the code

0
source

Source: https://habr.com/ru/post/1394594/


All Articles