We have a test tool we run every day that was crashing in certain circumstances. Specifically, it reads from an XML file and it was somewhere in this parsing the crash was happening. A little bit of investigation showed that if an XML node was missing an attribute, the application would crash.

As it turns out, I own the XML file the application reads, so I made sure all required attributes were in it. This has kept the crash from happening for the last few months. I filed a bug for me to fix that listed the problem but had not gotten around to fixing it until very recently. This did not concern me too much. In the big scheme of things, since I control the input XML file I could prevent the crash so I was not treating this as a high priority item. In test lingo, there was a "workaround" to the bug - just double check the XML file - so we can use that workaround in order to get our testing done.

Still, I did not like the idea of the crash so I dug around in our code (and then someone who knew the code better than I swung by and showed me where to look :) ). Here's approximately what I saw (cleaned up to make this much more readable):

XmlNode node = "<Person firstName='John' lastName='Guin'> "</Person>"; //not quite correct, but you get the idea

XmlAttributeCollection attrColl = node.DocumentElement.Attributes;

//These next two lines may crash
string _firstName = attrColl["firstName"].Value;
string _lastName = attrColl["lastName"].Value;
string fullName = _firstName + _lastName;

So what was happening is that the XmlNode would be missing either the firstName or lastName attribute and this would cause the crash. Obviously, there was no check to see if the attribute firstName or lastName was even present, and if it was not there, the .Value property would return an exception and cause a crash.

The fix was relatively easy. I just added a quick check to make sure the attribute was present before checking its value:

if( attrColl["firstName"] != null)

{
string _firstName = attrColl["firstName"].Value;
Log.Write ("Setting first name to " + _firstName);
}
else { //log that the attribute was missing}

and the same for the lastName. I could also have put this in a try statement.

A simple fix for a bad, but seldom hit, testing bug. It is also a fair example of a case in which a pretty severe bug is not necessarily a high priority work item.

Questions, comments, concerns and criticisms always welcome,

John