Michal Čihař - Fun with MinGW64 and ODBC

Fun with MinGW64 and ODBC

The test suite for recently added ODBC support in Gammu was consistently failing on Win64 platform (all others were just fine). It took me some time to figure out why. The reason is SQLLEN type, which is in some sort special, that generated code for it is broken.

All started with pretty much standard retrieving value from ODBC result:

SQLLEN size;
SQLRETURN ret;

/* Figure out string length */
ret = SQLGetData(res->odbc, 1, SQL_C_CHAR, NULL, 0, &size);
if (!SQL_SUCCEEDED(ret)) {
    return NULL;
}

/* Did not we get NULL? */
if (size == SQL_NULL_DATA) {
    return NULL;
}

/* Allocate buffer for string */
out = reallocate(out, size + 1);

The first problem was comparing of size against SQL_NULL_DATA. It simply always failed even if size did contain -1 (what is actually value of SQL_NULL_DATA). Yesterday, I've come out with typecasting magic that worked fine for this case.

However what I've missed is that even reallocate is failing with this parameter. No matter what the size would be, it would return NULL. Typecasting again did help here. Hey, should not be compiler clever enough to typecast this for me? It turns out that in case of MinGW64 it is not. Anybody has suggestion why?

At the end I ended up with pretty ugly duplicating of the variable just to avoid mixing it up in future changes (as would happen with typecasting everywhere).

I'd really like to know why my assumptions are wrong in this case.