One of the most important things to do when a program is finished is to run checks to be as sure as possible that there are no memory leaks or buffer overflows/underruns. Not only does this lower the chance for your program to crash, it lowers the chance of someone finding an exploitable vulnerability that could do any number of things from crashing the program to providing access to the system. In the programming world of open source and Linux, there are several options for checking your code against this type of flaw.
These errors are caused by programming mistakes, and they happen. No one writes perfect code 100% of the time so everyone should run some type of tests against their programs. I've recently been testing a program with valgrind and electric fence to try and find the cause of a specific error that occurs on a specific platform. Alas, the problem has not been found but I did learn quite a bit about how to test for programming flaws.
Valgrind should be run on all your program code. It checks for memory leaks and helps you determine where the errors may be. Using this is pretty straightforward, Just run valgrind <binary> and it will log all the memory leaks as they happen.
If you know your program has some flaws and you just can't find them, electric fence may be able to lend a hand. Electric fence is used in combination with gcc and gdb to attempt to find the exact line of code where a malloc() overrun or underrun occurs. While it doesn't always provide a sure-fire explanation of what's going on, it can provide some valuable information about where the problem could reside. To use electric fence, link efence with -efence in your build line. The code is then compiled and a binary is created. Then call gdb <binary> and type run at the gdb shell and recreate the conditions that cause your program to crash.
There are other options as well. gcc has its own malloc() debugger built in, but I won't be documenting it here. If you need more information, a quick Google search can provide much more information about either of these solutions. Valgrind and electric fence have been highly recommended to me lately and they've been very useful. Perhaps you can find them as useful as I have.

Must be logged in to post comments