tomlxq / pyodbc

Automatically exported from code.google.com/p/pyodbc
MIT No Attribution
0 stars 0 forks source link

Retrieving XML data from SQL Server via FreeTDS yields a bus error #136

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Use pyodbc to access a SQL Server database which has a table <tbl> which 
contains a column <col> which has the xml datatype.
2. c.execute("select top 1 <col> from <tbl> where <col> is not null")
3. c.fetchall()

What is the expected output? What do you see instead?

I expect to see the contents of the field. Instead, I get a bus error.

What version of the product are you using? On what operating system?

pyodbc 2.1.8
MacOS X 10.6.4
Python 2.6

Please provide any additional information below.

FreeTDS CVS checkout from 11/16/2010
iODBC driver manager
SQL Server 2008 RTM

gdb tells me that the bus error is 

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000004
0x0038fabf in GetDataString (cur=0x7028e0, iCol=<value temporarily unavailable, 
due to optimizations>) at pyodbc-2.1.8/src/getdata.cpp:194
194         if (PyString_CheckExact(bufferOwner))

I suspect that this is related to issue 97. My value for sys.maxunicode:

65535

For what it's worth, the minimum datalength in the table I'm accessing is 1826.

Original issue reported on code.google.com by s...@mitre.org on 17 Nov 2010 at 8:08

GoogleCodeExporter commented 8 years ago

Original comment by mkleehammer on 20 Nov 2010 at 7:04

GoogleCodeExporter commented 8 years ago
Just an update: FreeTDS 0.91 always returns the same length for XML data, so it 
is impossible to know the real length.  It seems to return the maxtext value, 
which in my case is 2147483647.

It happens to work when the XML length <= 1023 bytes because pyodbc uses a 1024 
byte stack buffer.  If the value is larger, it needs to allocate memory based 
on the size the driver reports.

I'm digging through the FreeTDS source, but I don't see a quick fix yet.

Original comment by mkleehammer on 29 Sep 2012 at 7:48

GoogleCodeExporter commented 8 years ago

Original comment by mkleehammer on 29 Sep 2012 at 7:48

GoogleCodeExporter commented 8 years ago
My previous comment was incorrect.  The length is not always the same.

Using FreeTDS 0.91 and pyodbc 3.0.6+, the problem is that a NULL terminator 
sometimes overwites the last byte.  Still working on it.

Original comment by mkleehammer on 30 Sep 2012 at 12:52

GoogleCodeExporter commented 8 years ago
I've raised two issues on the FreeTDS mailing list:

1) FreeTDS reports the column as SQL_CHAR instead of SQL_SS_XML
2) SQLGetData always returns SQL_SUCCESS, even if the buffer size is too small.

This means pyodbc 3.0.6+ is going to return XML as ANSI text and will work as 
long as the text is less than 1024 characters (the internal buffer size used 
for the first select).

In the meantime, you might consider using the Microsoft SQL Server Driver for 
Linux.  It works extremely well.

Original comment by mkleehammer on 30 Sep 2012 at 2:27