Posts

Showing posts from September, 2017

Be careful when you cast

There was a bug which indicated itself when something was saved to sqlite and then queried back. It turned out that we were saving int which got bind to int64 but when binding value in query we (tada!) used unsigned int!

The catch could be demonstrated with the following small example:

#include <iostream>
#include <stdint.h>

using namespace std;

int main() {
  int src = 0xFFFFFFFF;
  int64_t x = src;
  uint32_t req = 0xFFFFFFFF;
  int64_t y = req;
  cout << "x = " << hex << x << endl;
  cout << "y = " << hex << y << endl;
  cout << (x == y) << endl;
}

x = ffffffffffffffff <- what we are saving
y = ffffffff <- what we are searching for
0

In here, x and y get literally the same value but after converting to int64, totally different values get saved to the database!
Fix? Bind to int - it's 4 bytes and in this case! And maybe use uints/int more consistent;)