I was recently asked to hunt a bug in some of my old code. It was a shell script generating Postscript, generating .PNG files for a website. It had “organically” grown over some years and started generating bad Postscript starting May 1st. Since Postscript is well designed and has good error handling, it did not take long to find the problem. The word May had not been replaced by a 120 (the year day number of May 1st). Here is a sniped:
Note the ‘r’ that should be a ‘y’ in the center LOC. My bad. However, I realized that, in theory, this error could have been caught (by sed) because the second /[Mm]ar[^ ]*/ would not ever be satisfied. Sadly, I can’t make sed find my bad code, but I can dream, can’t I?
Ok, it was 1 am as I switched off the TV and only than I remembered I had to fix the flat tire on my kid’s bike. No biggy, this would only take 20 minutes, so I get my gear and start to work. Bummer, I need to take out the wheel, the leak is too small to hear. Ok, maybe 30 minutes, no worries. So I disengage the hub dynamo, noting the plus wire has a white stripe on it. I find the hole, fix it, reassemble the tire, and in my head I am already in my bed, in just 2 more minutes everything is properly reaffixed. I test the light as a matter of double compulsion. After all I am both a coder as well as a caring father. The light refuses to work. Maybe the connector is dirty. I start debugging, working my way up from the hub to the lamp. Twenty minutes later, I have the whole thing off the bike, stripped naked on the table and I take my multimeter to it. I curse these digital IC controlled lights. It used to be so simple. Just one wire connecting a dynamo and a bulb. (Or spit into the carbide lamp.) Whoever asked for a “wunderlamp”? After an other five minutes, I find out the bulb looks ok, but the meter tells me it’s broken. Curses! Why did my kid not tell me her light needed fixing? I would have checked the bulb first thing. Because of the flat tire I started debugging at the wrong end. Because I assumed the light was working before, I assumed the connector was the most probable thing to be at fault. That good all MOAFU called assumption. It is bad enough that I have to endure bad error propagation at work, but from my own blood?
Clock or modular arithmetic is hard. Take a look at the two code snippets below. Both seem reasonable integer comparison functions modeled after strcmp(). However, the left one contains an overflow bug.
I have thirty odd years of programming experience, yet I had to be pointed out the bug. The left snippet looks obviously wrong to me, now. It’s like claiming that we go back in time if the short hand of a clock moves more than 6 hours. Ridiculous. (And, no, intermediate casting to long won’t help.) Just try a few extreme cases using INT_MIN, for example. Clock arithmetic is hard because if the modulus is sufficiently large we tend to ignore it. So, if you have to go against the first directive, if you don’t trust the -O4 option, or if you just have to show off how brilliant you are, use something like the C code on the right. It won’t bring you much, but at least it does not contain an overflow bug. Though it might contain an other one, or show a incompatibility in your compiler. But that is left as an exercise to the reader.
Remember those days? About twenty seven years ago? You were on a role with Donkey Kong, playing at some high level—level twenty two, actually. And boom, time is up, you’d be dead. That is, Jumpman (“Mario,” to you DSi wielding youngsters) would be dead. Dead as a doornail, how unfair! My, then, therapist told me, that it was not as important as I thought it was, the moron. That I would forget it eventually, and go on with my life, double moron. How can I forget? I still cry myself to seep every night over how unfair life is. But now, thanks to Don Hodges, the world (“my world” as my current therapist refers to it) is back in harmony again. Thanks Don, for fixing Donkey Kong. I can sleep in peace, now.
This is what the (overflow) bug looks like:
0F7F 17 RLA ; Rotate Left the bits in A
0F84 80 ADD A,B ; A = A + B
0F85 80 ADD A,B ; A = A + B
0F86 C628 ADD A,#28 ; A = A + #28 (40 decimal)
0F88 FE51 CP #51 ; Is A >= #51 (81 decimal) ?
0F8A 3802 JR C,#0F8E ; No, then skip ahead to #0F8E
0F8C 3E50 LD A,#50 ; Yes, then A = #50 (80 decimal)
“I use the following bit of CSS to help visualize the structure of an XHTML (or HTML) document by putting a colored outline around the border of every element. At each level in the hierarchy the color changes so you can see when “depth” changes.
Let me correct myself on that, we /should/ be able to implement this with
dtrace on Mac OS X quite easily. However, dtrace is kind of like looking at
a beautiful painting through a kids kaleidescope toy.” (nemo) [The Objective-C Runtime: Understanding and Abusing]
If you are only interested in the above text you are a halfling, if you just hit the link to RTFA, you are a nerd, if you think you could write the article, you are a script-kiddy, and if you go WFT, you are a n00b. But if you do read it, you might learn some new debugging techniques.
Are you a lead developer? Do you feel commercially trapped? Would you like your own company for your brainchild? Are you too risk averse to start it? Change the rules! Start by visiting 4F Invest. You'll find it interesting.