Thursday, June 16, 2005

Oh my god! (and some news at the end)

I was really horrified to see this really blatant mistake in production quality code! Usually, bugs in a seasoned product tend to be subtle in nature and more of the logical errors that might have creeped in. But to make a mistake with the language feature itself and to have gone unnoticed for something like 3 years is deplorable! Just to give a simplified example of what the mistake was like...

Someone is calling a function that would count the number of 2 different items and returns their values in the 2 pointer arguments passed.

unsigned itemA = 0;
unsigned itemB = 0;
countItems(&itemA, &itemB);


The function itself goes something like this. My feeling is that the guy who wrote the functions got confused between pointers and references.

void countItems(unsigned *itemA, unsigned *itemB)
{
  // BUGBUG - setting pointer to 0!!!!
  itemA = 0;
  itemB = 0;
  // Should have been ...
  // *itemA = 0;
  // *itemB = 0;

  // in some loop
  {
    if(/* item is of type A */)
    {
     //BUGBUG - pointer arithmetic!!!!
     itemA++;
     // Should have been ...
     // (*itemA)++;
    }
    else if( /* item is of type B */)
    {
     //BUGBUG - pointer arithmetic!!!!
     itemB++;
     // Should have been ...
     // (*itemB)++;
    }
  }
  return;
}

The caller or the function itself never dereferenced the pointer and the caller merely used the value later, which would always be 0! And never dereferencing it meant the no one ever got any memory violations!

Anyway - programming horrors aside, this is a message to all my blog readers. I'm off to Kodai today. Will be back in 4 days. :)

Until then - P E A C E

7 comments:

  1. enjoy maadi :)
    4 days full maja

    ReplyDelete
  2. Kodai is a great place to visit....especially for nature lovers....dont forget to upload the photographs after you come.... have great time

    ReplyDelete
  3. arathi - thanks. will be back with loads of photos :)

    heap - yes, will upload them... :)

    ReplyDelete
  4. Silly error!! And it was ridiculus to see it in a deployed code!!

    Anyways.. have a blast there!!!Get back with loads of great snaps!!

    ReplyDelete
  5. The programmer must have thought the * to be an asterisk & perhaps has written a footnote - did u check? Isn't that a possibility?? :)

    ReplyDelete
  6. vaibhav - but any C programmer would know that * can be either a pointer descriptor or is present in a comment. And I did check the comments throughout the code snip. And there was no justification for that kind of usage.

    ReplyDelete

What I want to say is: