Thursday, March 16, 2006

Bug Oscars 2006

This happens to me about once every year or two. I come across the hardest or the weirdest bug I've ever faced in my whole life.

I still remember one of the first - in class 8, I was making a BASIC program to show large banners on the screen. At that time of course, I wasn't aware that the word "font" existed. My idea at that time was to print a normal line of text on the screen, read off the normal text one pixel at a time, and basically put n square pixels in the place of one pixel. I was lucky enough to have not only a computer, but a printer at my place at that time, so just after enlarging the text I could simply hit PrintScreen (which in those days actually used to print the screen!) and have a nice large banner in front of me. I still remember the first thing I printed - "I Beat Jaffar!" - Jaffar, for those who remember, being the evil Wazir in the very first edition of Prince of Persia.

The bug I was trying really hard to eliminate was that I didn't want the normal-size (small) line of text to be printed along with the large banner. After leafing through the GW-BASIC manual for a few days, I found the command that would allow me to not set the pixels of the normal text to black after I'd applied the enlargement. Not bad, for a kid of fourteen in the days of no Internet. Kids of age fourteen these days, of course, do far more practical things - like writing scripts to steal money online.

Today's bug - which won the top spot by making me sit at work until 3 AM on Holi, involved me trying to deploy a PHP webservice (a wrapper for an algorithm implemented in C++) on a much older system running Red Hat Linux 9 and PHP 4.2.2. Yeah, almost 20th century stuff. Don't ask me why. The code worked as expected on my Fedora Core 4 box with PHP 5.0.4; however I was also using quite a few programs (gnuplot and imagemagick to generate images, and the GNU scientific library) for which only older versions are installed on RH9. Anyway, after some testing and replacing some newer PHP functionality with old compatibility functions, I thought I was more or less done. Not by miles.

I was using PHP's exec() function to wrap system commands including my own compiled executable. The bug - half the system commands worked as expected, and the other half didn't. My own executable was running fine, taking its input and generating its output as necessary, but gnuplot and convert were just not working. Long (really long) story short, after wasting hours on the Internet, forums, PHP safe mode definitions, and resetting environment variables, I figured the problem was simply this. PHP5 on FC4 flushes its output buffer sooner than PHP4 on RH9. Of course, the fault was purely mine, since I'm supposed to flush buffers before using their output - but PHP doesn't exactly lend itself to code safety, and the code had worked flawlessly both on my Mac and Fedora - which of course, was the real problem... only after exploring every other option did I check if my code had gone wrong somewhere.

Moral of the story for me - don't be sloppy while coding, even if code still works. Moral of the story for developers - never, ever make it easier for dumb guys like me. Thats not progress, just a precursor to lazy (and ultimately destructive) coding habits.