[ee122] weird error, possibly stack corruption, need expert

vern at cs.berkeley.edu vern at cs.berkeley.edu
Thu Dec 6 21:29:14 PST 2007


> ... the simple receiver was working, then I
> added a declaration of a dummy variable in the main function, and the
> program stopped working.

That symptom almost always means that you either have a bug in which you're
reading uninitialized memory off of the stack (and the contents of that
memory have changed now that the stack layout has changed) or you are
overwriting a buffer on the stack (and now the exact effects of that have
altered since, again, the stack layout has changed).

> -Wall, and got a bunch of warnings about unused variables, and several print
> functions where I didn't print with the right flags (e.g. print void* with
> %x or %c, or something else with %ld, can this be the issue?).

*Do* fix those.  The particular ones you mention won't have caused this
problem, but other similar warnings certainly can.

> I checked the argument passed to sendto() namely the
> socket file descriptor, the sockaddr_in and the addrlen (didn't check the
> buffer and its size closely, but the buffer is not null, and the size is the
> right non-zero number). By printing these variables with gdb (and gdb on my
> computer let me see the content of the variable in a nice format with all
> the field names in front of the data), I can visually verify that the data
> passed to sendto() is the same data that was returned by the recvfrom() in
> main().

That may simply mean that the you're indeed reading the same area in memory
that your program overwrote earlier - there still could be a bug in your
sizing of variables (e.g., using sockaddr when you need sockaddr_in, or
having a pointer that you haven't initialized but happens to point somewhere
into the stack).

> Any clue about how to debug this thing or where the bug can be would be much
> appreciated.

Some first steps are to fix the warnings and carefully inspect your
initialization of variables, especially those that are used to return data
from library or system calls (such as strcpy(), recv() or getsockname()).

		Vern


More information about the ee122 mailing list