Skip to Content.
Sympa Menu

freetds - [freetds] Bug(s) with multi-byte results for SQLGetData

freetds AT lists.ibiblio.org

Subject: FreeTDS Development Group

List archive

Chronological Thread  
  • From: "Rick Koshi" <freetds AT more-right-rudder.com>
  • To: freetds AT lists.ibiblio.org
  • Subject: [freetds] Bug(s) with multi-byte results for SQLGetData
  • Date: Fri, 11 Oct 2013 17:48:07 -0400


I've been trying to track down some persistent bugs in some of my PHP code,
and I believe I've isolated at least one to FreeTDS.

In particular, I believe that FreeTDS is incorrectly handling "multi-byte"
results for SQLGetData. The two cases with which I'm concerned are
binary results and multi-byte character set results.

The most definite example I've seen is when the calling program
invokes SQLGetData() where the result type is a binary type (e.g.,
XSYBVARBINARY), the destination buffer is a character type (usually
SQL_C_CHAR), and the destination buffer is of even length (e.g., 256).
In this case, FreeTDS returns 254 characters, plus a null termination.
Unfortunately, I believe ODBC requires (and I know that PHP expects)
255 characters, plus a null termination. There's no provision for
a partially filled result buffer, except of course for the case where
the result buffer is larger than the result data.

Of course, I can understand why FreeTDS does this -- the conversion to
hexadecimal implied by the binary result means that any odd number of
characters leaves the result position halfway through a byte. Unfortunate,
but that seems to be what is required by ODBC.

I suspect that the same problem will occur if the application is configured
to receive a multi-byte character set (such as UTF-8) and the result data
happens to contain a multi-byte character at a point such that it needs to
be split across calls to SQLGetData(). I have not confirmed this, but given
what I've seen of FreeTDS's internal state structures, I don't see how it
could be any other way.

The result of this behavior is that spurious characters are inserted
into the results at the application level, since the application expects
the destination buffer to be fully used (minus a single null at the end).
The spurious characters are usually, but not always, null. I'm not sure
why they aren't always null (it seems to me that they should be), but
I've observed that they are sometimes seemingly random. I'm sure there's
a root cause somewhere.

My question to the list: Has anyone solved this problem? If not, I suppose
I will need to make my own best effort, but I'm sure there are people far
more familiar with the code than I am, and I wanted to make sure that I'm
not duplicating effort.

Thank you for your time, and thank you in advance for any help you may offer.

-- Rick



  • [freetds] Bug(s) with multi-byte results for SQLGetData, Rick Koshi, 10/11/2013

Archive powered by MHonArc 2.6.24.

Top of Page