Skip to content

Add dynamic buffers in GetDiagRecs and GetErrorFromHandle and prevent buffer overflow on errors longer than 1024 characters #881

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

Closed
Closed
2 changes: 1 addition & 1 deletion src/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ static int GetDiagRecs(Cursor* cur)

ODBCCHAR cSQLState[6]; // five-character SQLSTATE code (plus terminating NULL)
SQLINTEGER iNativeError;
ODBCCHAR cMessageText[10240]; // PRINT statements can be large, hopefully 10K bytes will be enough
ODBCCHAR cMessageText[SHRT_MAX]; // PRINT statements can be large
SQLSMALLINT iTextLength;

SQLRETURN ret;
Expand Down
7 changes: 2 additions & 5 deletions src/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ PyObject* GetErrorFromHandle(Connection *conn, const char* szFunction, HDBC hdbc
SQLSMALLINT cchMsg;

ODBCCHAR sqlstateT[6];
ODBCCHAR szMsg[1024];
ODBCCHAR szMsg[SHRT_MAX];

if (hstmt != SQL_NULL_HANDLE)
{
Expand Down Expand Up @@ -317,14 +317,11 @@ PyObject* GetErrorFromHandle(Connection *conn, const char* szFunction, HDBC hdbc

static bool GetSqlState(HSTMT hstmt, char* szSqlState)
{
SQLCHAR szMsg[300];
SQLSMALLINT cbMsg = (SQLSMALLINT)(_countof(szMsg) - 1);
SQLINTEGER nNative;
SQLSMALLINT cchMsg;
SQLRETURN ret;

Py_BEGIN_ALLOW_THREADS
ret = SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, 1, (SQLCHAR*)szSqlState, &nNative, szMsg, cbMsg, &cchMsg);
ret = SQLGetDiagField(SQL_HANDLE_STMT, hstmt, 1, SQL_DIAG_SQLSTATE, (SQLCHAR*)szSqlState, 5, &cchMsg);
Py_END_ALLOW_THREADS
return SQL_SUCCEEDED(ret);
}
Expand Down