-
Notifications
You must be signed in to change notification settings - Fork 7.9k
PDO_ODBC truncating CHAR field Db2i driver #18349
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
Comments
Yeah, this codepath in PDO_ODBC isn't ideal and we worry about regressions between different drivers a lot. That said, I haven't heard users I'm supporting having many issues recently outside of dealing with binaries (but they're also using the Db2i on IBM i, so the driver may behave differently there). I'm still interested in GH-10809 - if it works for you, it'd be a good idea to try to get it merged. I was a bit hesitant due to performance and compatibility concerns (as Db2i is the driver I'm most familiar with, I would like to know if impacts other drivers). Of course, there's also the possibility of a driver bug. I'd recommend checking both the unixODBC trace and the Db2i driver trace as well. |
Excellent, thanks for the quick response. I'll grab those traces in a bit, appreciate the direction. |
ok, here's the traces https://gist.github.com/fleadram/a3a07397f647413ba25e8b4b7fb95340 from my untrained reading, I don't see anything too suspicious in the odbc trace, but I see several
in the iaccess trace. Also, tomorrow I'll try patching in the above PR and test that. |
FYI, the duplicate CWB0111 are just a consequence of trying to convert from CCSID 37 to 1208 (UTF-8). The way the driver does this is by first converting from 37 to UTF-16 (13488), then from UTF-16 to UTF-8 so each step gives a buffer too small error. The ODBC trace is more baffling, since the string length output looks all kinds of wrong:
This length should be the total length required, but it returns 256 on the first run. That should have returned 255 bytes plus null terminator, so that would indicate that it returned all the data. The second run returns the same, so another 255 bytes. Finally the last one returns 50, so there should be 49 bytes plus null terminator. This totals 559 bytes, one less than the actual length of 560. This definitely seems a problem with our driver. I know I made changes in the latest update to fix this behavior for LOB locators, but I guess I need to do the same for inline conversions. The output of the last SQLGetData call also seems to not be null-terminated:
The previous outputs show the hexdump ends with @NattyNarwhal Does pdo_odbc use these lengths to construct the length of the PHP variable it creates? |
@kadler thanks for chiming in, while I'll defer to @NattyNarwhal for sure on the guts of the php, I think I would agree with your statement that ` /* fetch it into C->data, which is allocated with a length
|
It's been a while since I looked at this part of PDO_ODBC, but I believe it should return the full length left on the initial fetch of 256, yes. The driver does accommodate for this situation by calling in a loop until NO_DATA (as MS recommends), but the length calculation could be pretty fiddly due to things like calculations of length between the encoding it's in vs. requested. I know that PR I authored a while back was motivated by such a case (especially due to shift states in DBCS encodings). |
@fleadram Sorry to ping a month later; did you get a chance to try that PR and see if it resolved the issue? |
@NattyNarwhal sure did, and it does work! my only concern would be re what @kadler said above, if the driver is reporting the wrong column length could we still run into some edge case issues with really large columns i.e. > 2048 ? |
Description
The following code:
Resulted in this output:
But I expected this output instead:
I've seen a lot of discussion around pdo_odbc and long field fetching, but nothing that exactly matched what I'm seeing here. Similar though. We have a field that's defined in ddl as
CHAR(560)
on db2 for i (using up to date drivers from here). After upgrading our php from7.0.1
to8.3.20
we're having the issue where this column is only returning 256 chars, and the data returned is not positionally correct (i.e. the chunks seem to get overlaid on one another).The problematic logic seems to be the chunk approach for retrieving "long" columns starting here
I was able to debug, and patched in @NattyNarwhal 's printfs from here https://gist.github.com/NattyNarwhal/278aedf8e3c48bb8dcb4ae14cde0b283#file-debugging-printf-patch
What I see in both cases is that the driver is fetching a length of 256 at first, but the logic to continue the loop is (correctly) triggered 2x
What I see happening as I step through is that though the
original_fetched_len
is 256, the status is stillSQL_SUCCESS_WITH_INFO
causing it to drop into this logic block that overlays the result with the new data.Interestingly, if we patch a rollback of this commit, eliminating the recalculation of
used
, things go back to working.I also suspect that this PR would fix it too, but I don't know if that's still planned to go in.
Here's the logging output from the patch above.
PHP Version
PHP 8.3.20 (cli) (built: Apr 8 2025 20:21:18) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.3.20, Copyright (c) Zend Technologies
with Zend OPcache v8.3.20, Copyright (c), by Zend Technologies
with Xdebug v3.4.2, Copyright (c) 2002-2025, by Derick Rethans
Operating System
Red Hat Enterprise Linux release 9.5 (Plow)
The text was updated successfully, but these errors were encountered: