Skip to content

Commit

Permalink
New pyodbc.drivers() buffer too small.
Browse files Browse the repository at this point in the history
The new drivers() implementation used SQL_MAX_DSN_LENGTH, which seems reasonable but is
entirely too small.  Changed to a 500-character buffer.  Also fixed a couple of potential leaks
in that code during exception handling.

Fixes #202
  • Loading branch information
mkleehammer committed Feb 25, 2017
1 parent 36c69a7 commit 5e39755
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/pyodbcmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,29 +453,37 @@ static PyObject* mod_drivers(PyObject* self)
if (henv == SQL_NULL_HANDLE && !AllocateEnv())
return 0;

PyObject* result = PyList_New(0);
Object result(PyList_New(0));
if (!result)
return 0;

SQLCHAR szDriverDesc[SQL_MAX_DSN_LENGTH];
SQLCHAR szDriverDesc[500];
SWORD cbDriverDesc;
SQLCHAR szDesc[200];
SWORD cbDesc;

SQLUSMALLINT nDirection = SQL_FETCH_FIRST;
SWORD cbAttrs;

SQLRETURN ret;
SQLUSMALLINT nDirection = SQL_FETCH_FIRST;

for (;;)
{
Py_BEGIN_ALLOW_THREADS
ret = SQLDrivers(henv, nDirection, szDriverDesc, _countof(szDriverDesc), &cbDriverDesc, szDesc, _countof(szDesc), &cbDesc);
ret = SQLDrivers(henv, nDirection, szDriverDesc, _countof(szDriverDesc), &cbDriverDesc, 0, 0, &cbAttrs);
Py_END_ALLOW_THREADS

if (!SQL_SUCCEEDED(ret))
break;

if (PyList_Append(result, PyString_FromString((const char*)szDriverDesc)) != 0)
// REVIEW: This is another reason why we really need a factory that we can use. At this
// point we don't have a global text encoding that we can assume for this. Somehow it
// seems to be working to use UTF-8, even on Windows.
Object name(PyString_FromString((const char*)szDriverDesc));
if (!name)
return 0;

if (PyList_Append(result, name.Get()) != 0)
return 0;
name.Detach();

nDirection = SQL_FETCH_NEXT;
}

Expand All @@ -485,7 +493,7 @@ static PyObject* mod_drivers(PyObject* self)
return RaiseErrorFromHandle("SQLDrivers", SQL_NULL_HANDLE, SQL_NULL_HANDLE);
}

return result;
return result.Detach();
}


Expand Down

0 comments on commit 5e39755

Please sign in to comment.