Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

fix segment fault when query param repr string is large #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Connection::Connection (UMConnectionCAPI *_capi)
m_errno = -1;
memcpy (&m_capi, _capi, sizeof (UMConnectionCAPI));
m_dbgMethodProgress = 0;
m_dbgFailedRecv = 0;
m_errorType = UME_OTHER;
}

Expand Down Expand Up @@ -736,6 +737,18 @@ void *Connection::query(const char *_query, size_t _cbQuery)
return NULL;
}

if (m_dbgFailedRecv > 0)
{
if (!recvPacket())
{
m_dbgMethodProgress --;
return NULL;
}

m_reader.skip(); // pop dirty packages sent succ but recv failed
m_dbgFailedRecv --;
}

size_t len = _cbQuery;

if (len > m_writer.getSize () - (MYSQL_PACKET_HEADER_SIZE + 1))
Expand All @@ -762,6 +775,7 @@ void *Connection::query(const char *_query, size_t _cbQuery)
{
PRINTMARK();
m_dbgMethodProgress --;
m_dbgFailedRecv ++;
return NULL;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class Connection
UMConnectionCAPI m_capi;

int m_dbgMethodProgress;
int m_dbgFailedRecv;

public:

Expand Down Expand Up @@ -145,4 +146,4 @@ class Connection
protected:
};

#endif
#endif
6 changes: 5 additions & 1 deletion python/umysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,11 @@ PyObject *EscapeQueryArguments(Connection *self, PyObject *inQuery, PyObject *it
if (PyUnicode_Check(arg))
cbOutQuery += (PyUnicode_GET_SIZE(arg) * 6);
else
cbOutQuery += 64;
{
int len = PyString_GET_SIZE(PyObject_Str(arg));
if (len < 64) len = 64;
cbOutQuery += len;
}

Py_DECREF(arg);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,15 @@ def testUnsignedInt(self):
self.assertEquals([values1, values2, values3, ], rs.rows)
cnn.close()

def testLongStrReprObjParam(self):
cnn = umysql.Connection()
cnn.connect (DB_HOST, DB_PORT, DB_USER, DB_PASSWD, DB_DB)
obj = range(1000)
result = cnn.query("SELECT '%s'", (obj,))
self.assertEqual(result.rows[0][0], str(obj))
cnn.close()


if __name__ == '__main__':
from guppy import hpy
hp = hpy()
Expand Down