Bug #54206 Column octet length includes terminating null byte
Submitted: 3 Jun 2010 14:48 Modified: 13 Aug 2010 14:35
Reporter: Lawrenty Novitsky Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / ODBC Severity:S3 (Non-critical)
Version:3.51,5.1 OS:Any
Assigned to: Lawrenty Novitsky CPU Architecture:Any
Tags: SQLColAttribute SQL_DESC_OCTET_LENGTH t

[3 Jun 2010 14:48] Lawrenty Novitsky
Description:
SQLColAttribute for SQL_DESC_OCTET_LENGTH returns lehgtn including terminating null byte

Code exerpt:
case SQL_DESC_OCTET_LENGTH:
/* Need to add 1 for \0 on character fields. */
  *NumericAttributePtr= get_transfer_octet_length(stmt, field) +
  test(field->charsetnr != 63);

According to http://msdn.microsoft.com/en-us/library/ms713558%28v=VS.85%29.aspx
"The length, in bytes, of a character string or binary data type. For fixed-length character or binary types, this is the actual length in bytes. For variable-length character or binary types, this is the maximum length in bytes. This value does not include the null terminator."

3.51 driver's code contradicts with the quote above

How to repeat:
The bug can be illustrated by existing tests t_bug27862_1 and t_bug27862_2 from my_types suite

Suggested fix:
Just never add that 1 to columns octet length
[3 Jun 2010 16:29] Lawrenty Novitsky
the patch

Attachment: bug54206.diff (application/octet-stream, text), 1.89 KiB.

[3 Jun 2010 16:39] Lawrenty Novitsky
I have Bogdan's permission to tick review box on his behalf since we have discussed the issue with him and the patch is pretty simple. but still i'm leaving this honor to him.
btw i have concerns about SQL_DESC_DISPLAY_SIZE and SQL_DESC_LENGTH too. gonna check those too.
and i'm frightened a bit by simplicity of the patch :)
[3 Jun 2010 18:32] Jim Winstead
Patch looks fine. but I seem to recall that there was at least one instance where ODBC returned a length that did include the terminator, and it's possible that this just got carried over from that. I can't find what that might have been now.
[3 Jun 2010 22:41] Lawrenty Novitsky
Pushed as rev#713. Will go to the 3.51.28 (if everything goes well)
[18 Jun 2010 14:41] Tony Bedford
Added an entry to the 3.51.28 changelog:

SQLColAttribute for SQL_DESC_OCTET_LENGTH returned length including terminating null byte. It should not have included the null byte.
[23 Jul 2010 10:44] Bogdan Degtyariov
same is for MyODBC 5.1
[23 Jul 2010 14:45] Lawrenty Novitsky
Bogdan, I think i saw it in 5.1 too and even already fixed as part of work on one patch. but thank you for reminding about this bug. at least testcase needs to be merged to 5.1
[13 Aug 2010 14:35] Tony Bedford
Updated to also appear in 5.1.7 changelog.