Skip to content

Commit

Permalink
Fix for CONPY-175:
Browse files Browse the repository at this point in the history
Since memory for stack allocation is limited, we need to allocate
memory from the heap, otherwise in case of large strings escape_string
method might crash.
  • Loading branch information
9EOR9 committed Nov 2, 2021
1 parent d3eafac commit eb48926
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion mariadb/mariadb_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,13 @@ static PyObject *MrdbConnection_escape_string(MrdbConnection *self,
return NULL;

from= (char *)PyUnicode_AsUTF8AndSize(string, (Py_ssize_t *)&from_length);
to= (char *)alloca(from_length * 2 + 1);
if (!(to= (char *)PyMem_RawCalloc(1, from_length * 2 + 1)))
{
return NULL;
}
to_length= mysql_real_escape_string(self->mysql, to, from, (unsigned long)from_length);
new_string= PyUnicode_FromStringAndSize(to, to_length);
PyMem_Free(to);
return new_string;
}
/* }}} */
Expand Down
9 changes: 9 additions & 0 deletions testing/test/integration/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,14 @@ def test_server_status(self):
con.autocommit= False
self.assertTrue(not con.server_status & STATUS.AUTOCOMMIT)

def test_conpy175(self):
default_conf= conf()
c1 = mariadb.connect(**default_conf)
str= '"' * 4194304
newstr= c1.escape_string(str);
self.assertEqual(newstr, '\\"' * 4194304)
c1.close()


if __name__ == '__main__':
unittest.main()

0 comments on commit eb48926

Please sign in to comment.