Nasty bugs: std::logic_error because of NULL string

I recently changed the Warning::getConditions() method of ScopePort. It now takes two parameters:

  • string hostid - The ID of the host on which the sensor to check is running
  • string st - The ID of the sensor to check.

I started up ScopePort and got the following error after about 2-3 minutes:

terminate called after throwing an instance of ’std::logic_error’
what():  basic_string::_S_construct NULL not valid

Because of the time delay I searched for the error in the recently changed Warning::getConditions() method. I expected that an usual alarm was causing something to fail. But as my investigations went on I noticed that the time delay was exactly 180 seconds. Each time. So I thought about it and remembered that the onlineStateChecks() thread was going into full operations after 180 seconds (That is to give the clients time to send data.). I quickly found out where the error was caused: Just at the time of the Warning::getConditions() method call. The debugger didn’t even let me step into the method. It failed right at the method call. As it was pretty late and I had to get up early the next day I went to sleep and planned to ask a friend the next day.

The next morning. At the time I was explaining my Problem to the friend I already found the error. (Sometimes it is really better to go to bed…): When I updated the onlineStateChecks and clientHandler() threads to use the new method I made a mistake: I used the wrong column of the database result. This ended in 0 as second parameter. Exactly. Not “0″ but 0.

My friend explained me that the compiler was not complaining because he just accepted the 0 as a NULL string. I didn’t even look for wrong parameter types because I thought the compiler wouldn’t even think about compiling 0 as string. Lesson learnt. Thank you.

Here is a little example program that reproduces the error:

#include "TestClass.h"
using namespace std;

int main(){
    TestClass test;
    test.testMe("foo", 0);
    return 0;
}

class TestClass {
     public:
         void testMe(string foo, string bar){
              return;
         }
};
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • LinkedIn
  • Spurl
  • StumbleUpon
  • Technorati
  • Yigg

Tags: , , , , ,

Leave a Reply