views:

1888

answers:

7

In C++, in which way the stack may get corrupted. One way I guess is to overwriting the stack variables by accessing an array beyond its boundaries. Is there any other way that it can get corrupted?

+7  A: 

The C++ standard does not define stack/heap. Further, there are a number of ways to invoke undefined behavior in a program -- all of which may corrupt your stack (it's UB, after all). The short answer is -- your question is too vague to have a meaningful answer.

dirkgently
Perfectly answerable, just need to not be an academic.
soru
+6  A: 
  1. You could have a random/undefined pointer that ends up pointing to the stack, and write though that.
  2. An assembly function could incorrectly setup/modify/restore the stack
  3. Cosmic waves could flips bits in the stack.
  4. Radioactive elements in the chip's casing could flip bits.
  5. Anything in the kernel could go wrong and accidentally change your stack memory.

But those are not particular to C++, which doesn't have any idea of the stack.

Douglas Leeder
I think you mean that they are *not* particular to C++ :)
Stephan202
Yes :-)... Inserted 'not'
Douglas Leeder
Good point mentioning 3. To safeguard from this, I have just moved my computer so that it sits under my desk and is therefore in the shadow. Any other precautions I need to be aware of? ;)
Adrian Grigore
Actually, the commonest source of ionising radiations affecting chips is the chip packaging itself - there is no escape!
anon
Remove the packaging with sandpaper. Quick!
Potatoswatter
+2  A: 

Calling a function with the wrong calling convention.

(though this is technically compiler-specific, not a question of C++, every C++ compiler has to deal with that.)

peterchen
+2  A: 

Violations of the One Definition Rule can lead to stack corruption. The following example looks stupid, but I've seen it a couple of times with different libraries compiled in different configurations.

header.h

struct MyStruct
{
   int val;
#ifdef LARGEMYSTRUCT
   char padding[16];
#endif
}

file1.cpp

#define LARGEMYSTRUCT
#include "header.h"

//Here it looks like MyStruct is 20 bytes in size    

void func(MyStruct s)
{
   memset(s.padding, 0, 16); //corrupts the stack
   return; //Will probably crash here as the return pointer has been overwritten
}

file2.cpp

#include "header.h"
//Here it looks like MyStruct is only 4 bytes in size.
extern void func(MyStruct s);

void caller()
{
   MyStruct s;
   func(s); //push four bytes on to the stack
}
Richard Wolf
+2  A: 

Taking pointers to stack variables is a good way:

void foo()
{
  my_struct s;
  bar(&s);
}

If bar keeps a copy of the pointer then anything can happen in the future.

Summing up: Stack corruption happens when there's stray pointers pointing to the stack.

Jimmy J
+2  A: 

This might help you. Refer Memory Corruption Part I—Stacks.

(Login or Subscribe to access full content of this book)

aJ
+1  A: 

Throwing an exception inside a destructor is a good candidate. It would mess up the stack unwinding.

daramarak