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

WJT's "Miscellaneous bug and pyodbc compatibility fixes" + extra unicode fixes #14

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

batterseapower
Copy link

This PR just consists of #13 plus one extra Unicode fix I found was necessary to prevent pypyodbc from core dumping with an error about a double free.

wjt and others added 6 commits September 29, 2014 09:46
NamedTupleRow and MutableNamedTupleRow depend on _ColBufferList being
populated, so without this change they do not work.
This is consistent with pyodbc.
I think out-of-the-box compatibility with pyodbc is a worthy goal. I
understand not wanting a hard dependency on 'recordtype' but at least
providing read-only named field access seems like an improvement.
I've run into memory corruption bugs with the following test program on
64-bit Linux, sizeof(wchar_t) == 4, sizeof(Py_UNICODE) == 4,
sizeof(SQLWCHAR) == 2.

    from pypyodbc import connect

    # Replace '...' with real values
    conn_string = 'DRIVER={SQL Server};SERVER=...;PORT=1433;UID=...;PWD=...;TDS_Version=7.1;APP=...;DATABASE=...'
    while True:
        connect(conn_string, ansi=False)

For me this typically causes libc to abort() complaining of memory
corruption, double-free, etc. within a few tens of iterations with
cpython (and quicker with pypy).

The bug seems to be in the conversion from Python string to
SQLWCHAR * when calling SQLDriverConnectW. Given the sizes above,
pypyodbc sets wchar_pointer = ctypes.c_char_p, then passes a
UTF-16-encoded byte buffer to it. ctypes.c_char_p null-terminates the
string when converting to a C array - but it only adds a single NUL
byte. unixODBC expects that argument to be a UTF-16 array, terminated by
two NUL bytes.

This fix is a bit of a hack, but it does work.

It might be clearer to define something akin to c_char_p for these
arguments, I suppose.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants