Evil Coding

I got assigned a difficult trouble ticket. The customer was reporting that a lot of garbage was being displayed on the screen. Initially a developer thought it was just the wrong version of some DLLs being installed. So they made the resolution be to reinstall the application. This did not correct the problem. The customer reopened the trouble ticket. We get dinged when that happens. My team lead assigned this to me.

Some other developers continued to believe this had to do with old DLLs being installed. I told them this might be the cause. However I needed to see it myself. I would not believe this unless somebody could duplicate the problem. Nobody was able to do that except the customer. I took a look at some log files. That did not help much. It seemed like the program got messed up immediately upon startup.

Another developer said he solved a similar problem before. He said he found an instance where the code takes a CString object, gets a constant pointer to the char data it holds inside, then casts away the const. Then the code would go ahead and write to that pointer. It just so happened that the CString object was empty. So the location in memory that has the empty string got corrupted. That just blew away my mind.

I went and searched through the code that gets executed early or frequently in our program. Sure enough there was another instance where the code does the exact same thing. The const was casted away. And the code started writing to the location identified by the pointer. Here is where this insanity actually turns funny. I saw a comment from the original developer who wrote this code. He admitted that he did not quite understand the CString class. That is why he was casting back to the familiar character pointer.

The good news is that the fix to this problem resolved a number of trouble ticket the customer had opened. These problems were ones that developers could not reproduce. They were instances where our application was behaving very strangely. In fact, another developer asked me to let him know how I was progressing with my trouble ticket. When I explained some of my findings, he recalled back to the similar bug he fixed before. And it turns out that this led me to finding the solution for the problems he was investigating. Teamwork does pay off.