-
Notifications
You must be signed in to change notification settings - Fork 562
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
Buffer overflow in cursor create_name_map #931
Merged
mkleehammer
merged 13 commits into
mkleehammer:master
from
juliuszsompolski:columnbufoverflow
Aug 16, 2021
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b8b005c
column name buffer overflow
juliuszsompolski da393c6
fix
juliuszsompolski 5574c7e
add tests - lets see if these databases actually handle such long col…
juliuszsompolski 2b738f8
remove unsupported tests
juliuszsompolski c72289a
fix
juliuszsompolski 033a9e4
remove Access, Informix, SQL DW tests
juliuszsompolski baf350c
whitespace
juliuszsompolski 5794b9b
whitespace cd
juliuszsompolski b550078
remove sqllite tests as well
juliuszsompolski 89d9a2d
check if maybe long query output columns work
juliuszsompolski 27278c4
Revert "check if maybe long query output columns work"
juliuszsompolski 12095f3
add spark tests
juliuszsompolski 24166ff
revert nameLen change
juliuszsompolski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,8 @@ static bool create_name_map(Cursor* cur, SQLSMALLINT field_count, bool lower) | |
|
||
bool success = false; | ||
PyObject *desc = 0, *colmap = 0, *colinfo = 0, *type = 0, *index = 0, *nullable_obj=0; | ||
SQLSMALLINT nameLen = 300; | ||
ODBCCHAR *szName = NULL; | ||
SQLRETURN ret; | ||
|
||
I(cur->hstmt != SQL_NULL_HANDLE && cur->colinfos != 0); | ||
|
@@ -148,20 +150,21 @@ static bool create_name_map(Cursor* cur, SQLSMALLINT field_count, bool lower) | |
|
||
desc = PyTuple_New((Py_ssize_t)field_count); | ||
colmap = PyDict_New(); | ||
if (!desc || !colmap) | ||
szName = (ODBCCHAR*) pyodbc_malloc((nameLen + 1) * sizeof(ODBCCHAR)); | ||
if (!desc || !colmap || !szName) | ||
goto done; | ||
|
||
for (int i = 0; i < field_count; i++) | ||
{ | ||
ODBCCHAR szName[300]; | ||
SQLSMALLINT cchName; | ||
SQLSMALLINT nDataType; | ||
SQLULEN nColSize; // precision | ||
SQLSMALLINT cDecimalDigits; // scale | ||
SQLSMALLINT nullable; | ||
|
||
retry: | ||
Py_BEGIN_ALLOW_THREADS | ||
ret = SQLDescribeColW(cur->hstmt, (SQLUSMALLINT)(i + 1), (SQLWCHAR*)szName, _countof(szName), &cchName, &nDataType, &nColSize, &cDecimalDigits, &nullable); | ||
ret = SQLDescribeColW(cur->hstmt, (SQLUSMALLINT)(i + 1), (SQLWCHAR*)szName, nameLen, &cchName, &nDataType, &nColSize, &cDecimalDigits, &nullable); | ||
Py_END_ALLOW_THREADS | ||
|
||
if (cur->cnxn->hdbc == SQL_NULL_HANDLE) | ||
|
@@ -177,6 +180,16 @@ static bool create_name_map(Cursor* cur, SQLSMALLINT field_count, bool lower) | |
goto done; | ||
} | ||
|
||
// If needed, allocate a bigger column name message buffer and retry. | ||
if (cchName > nameLen - 1) { | ||
nameLen = cchName + 1; | ||
if (!pyodbc_realloc((BYTE**) &szName, (nameLen + 1) * sizeof(ODBCCHAR))) { | ||
PyErr_NoMemory(); | ||
goto done; | ||
} | ||
goto retry; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think goto is justified here to not repeat that logic... |
||
} | ||
|
||
const TextEnc& enc = cur->cnxn->metadata_enc; | ||
|
||
// HACK: I don't know the exact issue, but iODBC + Teradata results in either UCS4 data | ||
|
@@ -289,6 +302,7 @@ static bool create_name_map(Cursor* cur, SQLSMALLINT field_count, bool lower) | |
Py_XDECREF(colmap); | ||
Py_XDECREF(index); | ||
Py_XDECREF(colinfo); | ||
pyodbc_free(szName); | ||
|
||
return success; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like in my previous PR, I prefer to do some defensive double +1 here to rather have a too big buffer, and space for an extra character (like a termination character) than a too small one.