Reverse Psychology

Everything at Henry's company revolved around contracts with vendors. The IT department had relied on the aptly named Contract Manager -- the sole remaining Visual Basic 6 client-server application -- to support that business for the past 12 years.

Contract Manager was a rotten apple in the IT department's orchard of .NET Web apps. Its picky nature kept the company one release behind the latest Windows version, just to be on the safe side. The system's logic was inefficient and tangled, making it a pain to develop and maintain.

The only reason anyone could figure it was still around was because, though old, Contract Manager rarely broke. Rest assured, though, on those rare occurrences when it did break, people noticed.

Break Big or Not at All
"The Contract Manager is crashing!" yelled a voice on the phone. "We can't add new contracts into the system! This is really critical!"

Henry was jarred upright. He wasn't used to these kinds of calls.

"Um, OK. Well, are you getting any error messages?" Henry stammered, but it was of no use -- the line was already dead. Call after call came in and played out the same way.

One user, who followed up with an e-mail after Henry ignored her phone calls, was kind enough to include a screenshot of the error, which pointed to a culprit:

 Integer Overflow on N_CONTRACT_ID.

Henry gulped.

He knew that in Visual Basic 6, integers overflow at 32,768. Could it be that users had managed to exhaust all of the possible Contract ID numbers? Henry quickly checked to confirm his suspicions and sure enough: the 32,768th contract was entered earlier that morning.

Henry knew he could easily fix the code to accept larger IDs, but there was a catch. The Contract Manager .EXE was compiled against a COM+ DLL. This meant Henry had a choice. Fix the COM+ DLL to allow for larger Contract IDs, recompile the client, and force everyone to upgrade the right way (and take forever) … or he could hack it.

Strike That -- Reverse It
Henry had a bold idea. While he was out of positive ID numbers, there were plenty of negative ones. He turned the sequence that generated Contract IDs around, so that it started at -1 and counted down, but that created a new problem.

Contract Manager had huge amounts of business logic in the database, and that meant that it was scattered in dozens of procedures, packages and views. The code to fetch the latest contract resembled the following:

WHERE q.n_contract_id = 
  (SELECT MAX(n_contract_id) FROM contract_rev cr WHERE cr.n_bid = q.n_bid)

So, Henry brute-forced it and replaced all of the SELECT MAXs with this code:

  WHEN MIN(n_contract_id) < 0 THEN MIN(n_contract_id)
  ELSE MAX(n_contract_id)

Terrible, kludgy and awful, but in the end, it worked! Henry had squeezed another decade out of Contract Manager. He just hoped the system wouldn't be around that long.

About the Author

Mark Bowytz is a contributor to the popular Web site The Daily WTF. He has more than a decade of IT experience and is currently a systems analyst for PPG Industries.

comments powered by Disqus


Subscribe on YouTube