Bug #55024 Wrong type returned by SQLColAttribute(SQL_DESC_PRECISION...) in 64-bit Linux
Submitted: 6 Jul 2010 10:59 Modified: 13 Aug 2010 15:08
Reporter: Bogdan Degtyariov Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / ODBC Severity:S3 (Non-critical)
Version:5.1.6 OS:Linux (x64)
Assigned to: Bogdan Degtyariov CPU Architecture:Any
Tags: 64-bit, SQL_DESC_PRECISION

[6 Jul 2010 10:59] Bogdan Degtyariov
Description:
SQLColAttribute(SQL_DESC_PRECISION...) function returns wrong type identifiers that have negative value:

#define SQL_LONGVARCHAR       (-1) returned 4294967295
#define SQL_BINARY            (-2) returned 4294967294
#define SQL_VARBINARY         (-3) returned 4294967293
#define SQL_LONGVARBINARY     (-4) returned 4294967292
#define SQL_BIGINT            (-5) returned 4294967291
#define SQL_TINYINT           (-6) returned 4294967290
#define SQL_BIT               (-7) returned 4294967289

Obviously, they are returned as 32-bit unsigned integer.
This only happens in 64-bit Linux.

How to repeat:
See the test case uploaded below.

Suggested fix:
just fix it
[7 Jul 2010 12:27] Bogdan Degtyariov
test case

Attachment: test55024.c (text/plain), 2.76 KiB.

[6 Aug 2010 9:28] Bogdan Degtyariov
patch

Attachment: patch55024.diff (application/octet-stream, text), 4.74 KiB.

[8 Aug 2010 20:00] Lawrenty Novitsky
Bogdan, I remember we discussed this bug and agreed on solution. But now i'm pretty sure i opinion on the reason of the bug was wrong. Now i'm not even sure that is a bug.
See on the http://msdn.microsoft.com/en-us/library/ms713558%28v=VS.85%29.aspx
"SQLColAttribute returns descriptor information for a column in a result set. Descriptor information is returned as a character string, a 32-bit descriptor-dependent value, or an integer value."

i think if you you pass in your testcase SQLINTEGER value to the SQLColAttribute things should work fine. It should(i didn't test - only looked the code:)) fails only if SQLLEN is no of the same size with SQLINTEGER, i.e. - on 64bit systems(i pushed the testcase and you can see that it fails on win64 as well).
This SQLColAttribute is some alchemy. My interpretation that SQLLEN* parameter doesn't have to be really pointer to a SQLLEN. Otherwise many things have little sense. They don't have much either. btw corresponding descriptor field is SQLSMQLLINT. but if one passes SQLSMALLINT pointer - driver should crash...
[9 Aug 2010 5:54] Bogdan Degtyariov
Lawrin,

Thanks for the comment, but we have to make some decision. Crashing driver is not good in my opinion... returning corrupted result is even worse.
[10 Aug 2010 10:35] Lawrenty Novitsky
the patch has been pushed as rev#908. it fixes treating numeric value buffer as 32bit on 64bit platforms. That's not quite obvious from the specs, but other drivers do so, and parameter type is SQLLEN* anyway. Thus we changed that too.
[13 Aug 2010 15:07] Tony Bedford
An entry has been added to the 5.1.7 changelog:

SQLColAttribute(SQL_DESC_PRECISION...) function returned incorrect results for type identifiers that have a negative value:

#define SQL_LONGVARCHAR       (-1) returned 4294967295
#define SQL_BINARY            (-2) returned 4294967294
#define SQL_VARBINARY         (-3) returned 4294967293
#define SQL_LONGVARBINARY     (-4) returned 4294967292
#define SQL_BIGINT            (-5) returned 4294967291
#define SQL_TINYINT           (-6) returned 4294967290
#define SQL_BIT               (-7) returned 4294967289

They were returned as 32-bit unsigned integer values. This only happened on 64-bit Linux.