Bug #34429 SQLGetData w/SQL_C_WCHAR gives incorrect data
Submitted: 8 Feb 2008 18:55 Modified: 28 Mar 2008 19:20
Reporter: John Water
Status: Closed
Category:Connector/ODBC Severity:S2 (Serious)
Version:5.01.02.00 OS:Microsoft Windows (XP)
Assigned to: Bugs System Target Version:
Tags: SQLGetData
Triage: D2 (Serious)

[8 Feb 2008 18:55] John Water
Description:
Hi All,

Thanks you for fixing bug# 32684.

I just down loaded a snapshot of the MySQL Connector/ODBC 5.1: Alpha for Windows (built on
02/04/2008) and tested it with 

our applications against MySQL 5.1.

However, SQLGetData now may give incorrect data in the second call to this function.  Here
is my test:

1) Create a table and populate this table:
create table test (pk int not null primary key, c1 varchar( 200 ));
insert into test values( 1, 'insert into test values( 1,
'abcdefghijklmnopqrstuvwxyz01234567898765a1b2c3d4e5f6g7h8i9jakblcmdneofpgqhrisjtk' );

2) Fetch the varchar column, c2 with
      wchar_t buff[32];
      SQLGetData( stmt, 2, SQL_C_WCHAR, buff, sizeof( buff ), &len );

Then SQLGetData will be called 3 times and we'll get 3 chunks of data.  The first chunk
seems okay and buff contains
      abcdefghijklmnopqrstuvwxyz01234
The second chunk would be
      4567898765a1b2c3d4e5f6g7h8i9jak
It is incorrect, because the first byte in the second chunk is the last byte in the first
chunk.

I have run my little repro against MS SQL Server, Oracle, Sybase ASE and MySQL and the
results are shown as below:

MS SQL Server
Test Begin ...
SQLFetch: ret = 0: row = 1, pk = 1, ind = 4
SQLGetData: ret = 1, len = 80, chunk = 1, buff = abcdefghijklmnopqrstuvwxyz01234
SQLGetData: ret = 1, len = 49, chunk = 2, buff = 567898765a1b2c3d4e5f6g7h8i9jakb
SQLGetData: ret = 0, len = 18, chunk = 3, buff = lcmdneofpgqhrisjtk
... Test End

Oracle
Test Begin ...
SQLFetch: ret = 0: row = 1, pk = 1, ind = 4
SQLGetData: ret = 1, len = 80, chunk = 1, buff = abcdefghijklmnopqrstuvwxyz01234
SQLGetData: ret = 1, len = 49, chunk = 2, buff = 567898765a1b2c3d4e5f6g7h8i9jakb
SQLGetData: ret = 0, len = 18, chunk = 3, buff = lcmdneofpgqhrisjtk
... Test End

Sybase ASE
Test Begin ...
SQLFetch: ret = 0: row = 1, pk = 1, ind = 4
SQLGetData: ret = 1, len = 80, chunk = 1, buff = abcdefghijklmnopqrstuvwxyz01234
SQLGetData: ret = 1, len = 49, chunk = 2, buff = 567898765a1b2c3d4e5f6g7h8i9jakb
SQLGetData: ret = 0, len = 18, chunk = 3, buff = lcmdneofpgqhrisjtk
... Test End

MySQL
Test Begin ...
SQLFetch: ret = 0: row = 1, pk = 1, ind = 4
SQLGetData: ret = 1, len = 80, chunk = 1, buff = abcdefghijklmnopqrstuvwxyz01234
SQLGetData: ret = 1, len = 49, chunk = 2, buff = 4567898765a1b2c3d4e5f6g7h8i9jak
SQLGetData: ret = 0, len = 18, chunk = 3, buff = blcmdneofpgqhrisjtk
... Test End

You'll see the results from MySQL are incorrect.

Thanks!

How to repeat:
A repro will be attached.  Here are the steps to repro:

1) Unzip the zip file to a directory and you'll see there are 3 files: odbcbug.c,
odbcbug.exe and output.
2) Run odbcbug.exe as
	odbcbug "dsn=your_dsn;uid=your_login_id;pwd=your_password"

Then you'll see the problem.
[8 Feb 2008 20:34] Jess Balint
patch + test

Attachment: bug34429.diff (application/octet-stream, text), 3.35 KiB.

[8 Feb 2008 20:36] Jess Balint
John,

Thank you for your bug report.
[17 Mar 2008 20:30] Jess Balint
Committed as rev 1077 and will be released in 5.1.3.
[28 Mar 2008 19:20] MC Brown
A note has been added to the 5.1.3 changelog: 

Using SqlGetData in combination with SQL_C_WCHAR would return overlapping data.