Expert C Programming

(Jeff_L) #1

Programming Challenge


Test Crash Your Software


Complete the test program fragments above.


Try running them to see how these bugs are reported by the OS.


Extra Credit: Write signal handlers to catch the bus error and segmentation fault signals.
Have them print a more user-friendly message, and exit.


Rerun your program.


The dereferencing of the illegal pointer value may be done explicitly in your code as shown above, or
it may occur in a library routine if you pass it a bad value. Unhappily, changing your program (e.g.,
compiling for debugging or adding extra debugging statements) can easily change the contents of
memory such that the problem moves or disappears. Segmentation faults are tough to solve, and only
the strong will survive. You know it's a really tough bug when you see your colleagues grimly
carrying logic analyz-ers and oscilloscopes into the test lab!


Software Dogma


A Segmentation Violation Bug in SunOS


We recently had to solve a segmentation fault that was occurring when the ncheck utility
was run on a corrupted filesystem. This was a very distressing bug, because you are most
likely to use ncheck to investigate filesystems that you suspect of corruption.


The symptom was that ncheck was failing in printf, by dereferencing a null pointer and
causing a segmentation violation. The faulty statement was:


(void) printf("%s", p->name);


Most junior programmers from Yoyodyne Software Corp. would fix this in a long-winded
way:

Free download pdf