[ee122] Segfault Madness (need expert)

Daniel Killebrew dank at eecs.berkeley.edu
Wed Oct 31 15:48:11 PDT 2007

It sounds like stack overrun to me to, so one thing that /may /work (I 
have never tried something like this). Hopefully if you compile in full 
debugging mode with no optimizations the compiler will preserve your 
stack ordering. So try this:

void SomeFunction()
int topStack = 0xfeedbabe;
<some other stack variables you actually care about>
int bottomStack = 0xdeadbeef;

<do stuff in your function>

<before you take any and *all *return paths from your function>
assert(topStack == 0xfeedbabe && bottomStack==0xdeadbeef);

It may work, shrug. Worth trying new stuff when hitting your head 
against a wall, I suppose. Hopefully it will detect if your function or 
any functions it calls are writing outside their stack space. You can of 
course make the top and bottom stack variables into arrays if you want a 
larger safety margin (in case the offending function might not be 
writing contiguous bytes).


vern at EECS.Berkeley.EDU wrote:
> Yes, this (unfortunately) is a classic C pointer error, where memory is
> getting overwritten and the problem only manifests later when the trashed
> value is accessed.  In Drew's code, adding the new variable changes the
> stack layout.  This suggests (but not definitively) that in this case the
> problem is something being overrun on the stack due to a local buffer,
> rather than a heap pointer managed by malloc/free.
> One way to try to find problems like this is to use gcc -g -Wall in order
> to catch problems that can be found at compile time, and then to execute
> inside of gdb, which will at least show the location of where the problem
> *manifests* (-g turns on debugging symbols).
> I believe the instructional machines also have some more powerful tools
> available such as Purify or Coverity.  But these will have a learning curve
> associated with figuring out how to use them.
> 		Vern
> _______________________________________________
> ee122 mailing list
> ee122 at mailman.ICSI.Berkeley.EDU
> http://mailman.ICSI.Berkeley.EDU/mailman/listinfo/ee122

More information about the ee122 mailing list