Monday, March 31, 2014

Programming errors cause program crashes in different places every single time

Problem
You are given the source to an application which crashes when it is run. After running it ten times in a debugger, you find it never crashes in the same place. The application is single threaded, and uses only the C standard library. What programming errors could be causing this crash? How would you test each one?
Solution
The question largely depends on the type of application being diagnosed. However, we can give some general causes of random crashes.

General cases
  • Not initialising a variable but attempting to use its value.
  • Dereferencing a null pointer.
  • Reading or writing past the end of an array.
  • disk full, i.e. other processes may delete a different file causing more space to be available
  • memory issue, i.e. other processes allocate and/or free memory. OutOfMemoryException in java can result due to this. But risk of such errors happening in c/c++ are higher, as programmer has to explicitly manage memory. Suppose, you don't free the pointer which will result in memory leak. If you free up the pointer, but you reuse the variable somewhere which results in dangling pointer.
  • a pointer points to a random location in memory that is changed by another process causing some values be "valid" (very rare though)
Specific to C
  • Defining a preprocessor macro that starts with an underscore and either a capital letter or another underscore.

 

Reactions:

0 comments:

Post a Comment