Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to insert in CHAR(1) UTF8 (and connection UTF8) throws string truncation error on native/embedded [JDBC233] #282

Closed
firebird-automations opened this issue Feb 6, 2012 · 14 comments

Comments

@firebird-automations
Copy link

Submitted by: @mrotteveel

Replaces CORE3701

Attachments:
core3701.zip

Votes: 1

See CORE3701 for original issue.
Using a PreparedStatement to insert a single character into a CHAR(1) field in a UTF8 database using the UTF8 or NONE connection characterset using native or embedded results in:
SQL error code = -303
arithmetic exception, numeric overflow, or string truncation
string right truncation

This problem does not occur with the pure-java implementation.

Commits: 91c11e8

@firebird-automations
Copy link
Author

Modified by: @mrotteveel

assignee: Roman Rokytskyy [ rrokytskyy ] => Mark Rotteveel [ avalanche1979 ]

@firebird-automations
Copy link
Author

Modified by: @mrotteveel

Version: Jaybird 2.1.6 [ 10285 ]

@firebird-automations
Copy link
Author

Modified by: @mrotteveel

Link: This issue replaces CORE3701 [ CORE3701 ]

@firebird-automations
Copy link
Author

Commented by: @mrotteveel

To check:
* Database characterset ISO8859_1 using NONE and UTF8 connection characterset

@firebird-automations
Copy link
Author

Commented by: @mrotteveel

With a database created with default characterset ISO8859_1, connection characterset NONE does work, UTF8 still returns an error.

@firebird-automations
Copy link
Author

Commented by: @mrotteveel

Simplified reproduction script using native library (see included readme.txt)

@firebird-automations
Copy link
Author

Modified by: @mrotteveel

Attachment: core3701.zip [ 12100 ]

@firebird-automations
Copy link
Author

Commented by: @mrotteveel

Found the cause. The data array in the XSQLVAR was shorter than the sqllen defined, either reducing the sqllen to the actual length or padding the data array seems to fix it.

Padding might be the required course of action, because I believe the XSQLVARs are reused for multiple executions, and reducing the sqllen would break things.

@firebird-automations
Copy link
Author

Commented by: @mrotteveel

Found a workaround: Cast the parameter to a VARCHAR of the same type
eg change:
INSERT INTO TEST (TEST_FIELD) VALUES(?)
to:
INSERT INTO TEST (TEST_FIELD) VALUES(CAST(? AS VARCHAR(1)))

@firebird-automations
Copy link
Author

Commented by: @mrotteveel

It looks like the code in xsqlda_wrapper.cpp should take care of padding, I need to find out why it isn't actually being executed for the SQL_TEXT.

@firebird-automations
Copy link
Author

Commented by: @mrotteveel

That padding code also contains a (potential) bug for character set NONE: It seems to be padding use 0x20 always, while I think NONE should pad with 0x00.

@firebird-automations
Copy link
Author

Commented by: @mrotteveel

Found the problem. The code for copying the Java XSQLVAR to a native XSQLVAR would first assign the (java) byte array size (two bytes) to the sqldata field, and then overwrite it with the bytearray. This poses no problem for any structure longer than two bytes, but in the case of single byte characters (in any encoding), this would cause a buffer overflow. In the case of single byte encodings this would not be a big problem (as the data area is 1 byte bigger then needed), but for UTF8 it would cause the padding to be overwritten by an unexpected byte value (eg for 'W' it would write out 0x57 0x00 0x20 0x20 instead of the expected 0x57 0x20 0x20 0x20.

I also expect this could introduce unexpected characters in CHAR fields with a size longer than 1 when a single character is assigned.

@firebird-automations
Copy link
Author

Modified by: @mrotteveel

status: Open [ 1 ] => Resolved [ 5 ]

resolution: Fixed [ 1 ]

Fix Version: Jaybird 2.2 [ 10053 ]

@firebird-automations
Copy link
Author

Modified by: @mrotteveel

status: Resolved [ 5 ] => Closed [ 6 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants