Electric Fence

Electric Fence helps you detect two common programming bugs: software that overruns the boundaries of a malloc() memory allocation, and software that touches a memory allocation that has been released by free(). Unlike other malloc() debuggers, Electric Fence will detect read accesses as well as writes, and it will pinpoint the exact instruction that causes an error. It is commonly used with a debugger (e.g., GDB) to detect where such memory violations occur.

The Electric Fence manual page suggests the following strategy for testing:
  1. Link with libefence.a as explained above.
  2. Run your program in a debugger and fix any overruns or accesses to free memory.
  3. Quit the debugger.
  4. Set EF_PROTECT_BELOW = 1 in the shell environment.
  5. Repeat step 2, this time repairing underruns if they occur.
  6. Quit the debugger.
  7. Read the restrictions in the section on WORD-ALIGNMENT AND OVERRUN DETECTION. See if you can set EF_ALIGNMENT to 0 and repeat step 2. Sometimes this will be too much work, or there will be problems with library routines for which you don't have the source, that will prevent you from doing this.