Bug #61873 SQLGetData returns incorrect length under 64 bit compile
Submitted: 14 Jul 2011 21:58 Modified: 10 Aug 2011 16:17
Reporter: Corwin Joy Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / ODBC Severity:S2 (Serious)
Version:3.51.26 OS:Linux
Assigned to: Bogdan Degtyariov CPU Architecture:Any

[14 Jul 2011 21:58] Corwin Joy
Description:
I am running MySQL connector installed from the RPM package which is version  3.51.26r1127-1.el5 
on a 64 bit machine.
If I call the function SQLGetData to retrieve a long string using the standard signature:
SQLRETURN SQLGetData(
      SQLHSTMT       StatementHandle,
      SQLUSMALLINT   Col_or_Param_Num,
      SQLSMALLINT    TargetType,
      SQLPOINTER     TargetValuePtr,
      SQLLEN         BufferLength,
      SQLLEN *       StrLen_or_IndPtr);

What I find is that the last parameter, StrLen_or_IndPtr, is not filled in correctly under a 64 bit compile. What is supposed to be returned under 64 bits is the length of the retrieved string as type SQLLEN and under 64 bits this resolves to a long integer (8 bytes long).  Instead what is returned is a 4 byte integer (an int type).  This means that the high order bytes are not being correctly set when this function is called and are just left uninitialized.

How to repeat:
Steps to reproduce:
*StrLen_or_IndPtr = 0xFFFFFFFFFFFFFFFF; // initialize all 8 bytes to 0xFF
SQLGetData(...); // call SQLGetData
// look at returned value for *StrLen_or_IndPtr, high order bytes uninitialized

Suggested fix:
Suggested fix:
My guess here is that inside the library the type being used is either not SQLLEN or somewhere a bad cast is done to the longer type.  If you like I can provide a standard example for calling SQLGetData but I'm sure you have a SQLGetData test already.
[17 Jul 2011 13:00] Valeriy Kravchuk
Are you sure your MyODBC version is 64-bit? How exactly you compiled your sample program? I'd prefer to see the exact command line.
[18 Jul 2011 16:40] Corwin Joy
I did not compile the MySQL Connector package myself.  Instead I am using the standard Redhat packages.  What I have installed is:

> yum info mysql-connector-odbc.x86_64

Installed Packages
Name       : mysql-connector-odbc
Arch       : x86_64
Version    : 3.51.26r1127
Release    : 1.el5
Size       : 450 k
Repo       : installed
Summary    : ODBC driver for MySQL
URL        : http://www.mysql.com/downloads/api-myodbc.html
License    : GPLv2 with exceptions
Description: An ODBC (rev 3) driver for MySQL, for use with unixODBC.

And I have the corresponding MySQL package for 64 bit.

> yum info mysql.x86_64
Installed Packages
Name       : mysql
Arch       : x86_64
Version    : 5.0.77
Release    : 4.el5_6.6
Size       : 8.1 M
Repo       : installed
Summary    : MySQL client programs and shared libraries.
URL        : http://www.mysql.com
License    : GPLv2 with exceptions
Description: MySQL is a multi-user, multi-threaded SQL database server. MySQL is
           : a client/server implementation consisting of a server daemon
           : (mysqld) and many different client programs and libraries. The base
           : package contains the MySQL client programs, the client shared
           : libraries, and generic MySQL files.

> yum info mysql-server.x86_64
Installed Packages
Name       : mysql-server
Arch       : x86_64
Version    : 5.0.77
Release    : 4.el5_6.6
Size       : 22 M
Repo       : installed
Summary    : The MySQL server and related files.
URL        : http://www.mysql.com
License    : GPLv2 with exceptions
Description: MySQL is a multi-user, multi-threaded SQL database server. MySQL is
           : a client/server implementation consisting of a server daemon
           : (mysqld) and many different client programs and libraries. This
           : package contains the MySQL server and some accompanying files and
           : directories.
[1 Aug 2011 18:08] Lawrenty Novitsky
I would be more interested to know if the problem repeatable with latest driver release, which is 3.51.28
[10 Aug 2011 13:04] Bogdan Degtyariov
Verified with 3.51.26
[10 Aug 2011 13:05] Bogdan Degtyariov
test case

Attachment: bug61873.c (text/plain), 2.77 KiB.

[10 Aug 2011 13:05] Bogdan Degtyariov
Test case displays:

sizeof(SQLLEN)=8
StrLen_or_IndPtr: [DEC: 18446744073709551615][HEX: FFFFFFFFFFFFFFFF]
GET DATA
StrLen_or_IndPtr: [DEC: 18446744069414588416][HEX: FFFFFFFF00001000]
[10 Aug 2011 13:07] Bogdan Degtyariov
This problem has been fixed in 3.51.28.
Here is the output from the same test case when using 3.51.28:

sizeof(SQLLEN)=8
StrLen_or_IndPtr: [DEC: 18446744073709551615][HEX: FFFFFFFFFFFFFFFF]
GET DATA
StrLen_or_IndPtr: [DEC: 4096][HEX: 0000000000001000]

Closing the bug
[10 Aug 2011 16:17] Corwin Joy
Thank you for looking into this!  Much appreciated and I'm glad to hear the bug is resolved in the new version.

Thanks again.

Corwin