Bruce got an urgent phone call from the Boss: one of the Big Customers was having some trouble with a system Bruce's company built, and it needed to be fixed right away. The application in question was the back-end for a wireless inventory management system and was responsible for maintaining the status of each location in the store in a flat text file.

Over the years, the application had been maintained by a contract programmer named "Fred" who had quite a few "nonstandard approaches" to application development. Being a contractor, his income was dependent on getting more contracts when the current contracts expired. His didn't accomplish this by writing good-quality code as to get repeat work and recommendations. Instead, he wrote code so awful that his victims would have no choice but to call on him to fix up the mess. Fred also had a background in COBOL programming, and it showed in his C code.

Only in Fred Code could one find such monstrosities as a function with an 86-character name and 57 characters of C code within the function. Which, as it happened, led to a rather strange bug caused by two such functions with similar names and the compiler's 32-character length limit for function names.

But on this occasion, the application was occasionally returning incorrect data from the flat file, so Bruce had to dig through several thousand lines of C code with some longwinded function names in CamelCase. Eventually, he discovered that the application was failing because it would occasionally write nulls to the text file, which caused problems when those lines were read back later.

All of the integer values that were written to the text file had to be padded out with zeroes so that the text file would be properly aligned. The way that Fred chose to do this was astonishing.

void ConvertLongToString(char NinecharString[], long Number)
{
  if (Number <= -10000000)
        sprintf(NinecharString, "%ld", Number);
  else if ( Number <= -1000000 )
        sprintf(NinecharString, "%d%ld",0, Number);
  else if ( Number <= -100000 )
        sprintf(NinecharString, "%d%d%ld",0,0, Number);
  else if ( Number <= -10000 )
        sprintf(NinecharString, "%d%d%d%ld",0,0,0, Number);
  else if ( Number <= -1000 )
        sprintf(NinecharString, "%d%d%d%d%ld",0,0,0,0, Number);
  else if ( Number <= -100 )
        sprintf(NinecharString, "%d%d%d%d%d%ld",0,0,0,0,0, Number);
  else if ( Number <= -10 )
        sprintf(NinecharString, "%d%d%d%d%d%d%ld",0,0,0,0,0,0, Number);
  else if ( Number <= 0 )
        sprintf(NinecharString, "%d%d%d%d%d%d%d%ld",0,0,0,0,0,0,0, Number);
  else if ( Number <= 10)
        sprintf(NinecharString, "%d%d%d%d%d%d%d%d%ld",0,0,0,0,0,0,0,0, Number);
  else if ( Number <= 100)
        sprintf(NinecharString, "%d%d%d%d%d%d%d%ld",0,0,0,0,0,0,0, Number);
  else if ( Number <= 1000)
        sprintf(NinecharString, "%d%d%d%d%d%d%ld",0,0,0,0,0,0, Number);
  else if ( Number <= 10000)
        sprintf(NinecharString, "%d%d%d%d%d%ld",0,0,0,0,0, Number);
  else if ( Number <= 100000)
        sprintf(NinecharString, "%d%d%d%d%ld",0,0,0,0, Number);
  else if ( Number <= 1000000)
        sprintf(NinecharString, "%d%d%d%ld",0,0,0 Number);
  else if ( Number <= 10000000)
        sprintf(NinecharString, "%d%d%ld",0,0 Number);
  else if ( Number <= 100000000)
        sprintf(NinecharString, "%d%ld",0, Number);
  else if ( Number <= 1000000000)
        sprintf(NinecharString, "%ld", Number);
}

Because Fred didn't write other functions that could write shorter strings, every single integer in the flat text file had a length of nine characters. Month number, sales tax percentage, weight in pounds, you name in.

Occasionally, this function would write nulls where digits should have gone, such as if a zero was passed as the number to write. I'll leave it as an exercise for the morbidly curious to discover the other bugs.

Bruce Not daring to do the sensible, one-line sprintf()-based solution, Bruce instead wrote a replacement that behaved exactly the same way. You never know, those embedded minus signs for negative numbers could have been important. It must have done the trick and then some as, the next day, the Big Company reported that a few other "too costly to fix, but easy to manually work around" bugs were finally fixed as well.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!