Search code examples
cstack-overflowbuffer-overflow

problem with ansi c unexpected behaviour?


I am suffering from an unexpected behavior; here is the problem definition:

I have applications communicating on a LAN via UDP protocol. I am reading the IP address and port number from a text file. Initially the IP address and port number are working nicely but, after some time, the IP address that is stored in a char array is corrupted and it takes garbage values. Also, the file writing is effected by this. I mean the values that are in the IP array are also written in text file that is written by the same application. I can't understand what is the problem - can you help?


Solution

  • This almost certainly means you have a buffer overflow - possibly even a stack overflow. You are probably reading too many bytes into too small an array of bytes, and running past the end of the allocated space and trampling over your other data (such as the IP address).

    If you are using Linux, consider using valgrind to help diagnose the problem.