Tuesday, January 19, 2010

A reallife story about return value

Today we detected a random failure in some of our regression scenarios. After an investigation in the traces, we found the problem actually came from a component we are using, which parses a string into tokens. The parser fails randomly when processing certain tokens.

As usual, the first suspect is a memory issue, but after a detailed inspection with gdb, we found nothing really harmful. Fortunately, the parser leaves a detailed error message when it stops and it seems that the function used to retrieve the token from the buffer encounters some problems.

Finally, after reading the user guide of the parser, we noticed that the function has to return a bool value indicating the parser if there is an error or not and it turned out this particular function does not have a return actually even it is defined to return a bool value.

In this case, the return value is a random value in the register. Actually, this is warned by the gcc.

Lesson learned here, always remember to return something for every branch in a function that is supposed to return something. Pay attention to compiler warnings.

-----------------------------------------------------------------------
Yet another story.
This time our soft encountered a failure. After investigation, the problem is caused by a misuse of the string::find(), similar to the following:
aPos = aStr.find(' ', 0);

if(aPos == string::npos)...

the aPos is declared as 32 bits unsigned integer, as the string::npos has value -1 but a special type string::size_type which is usually size_t, so the exact type depends on how size_t is defined by the compiler, the comparison will never succeed even if no space is found in the string on 64-bit systems, for example.

So the lesson is always use string::size_type for string positions.

No comments: