diff --git a/NEWS b/NEWS index f31a1f9a..01cee926 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,8 @@ What's new in psycopg 2.9.11 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Add support for Python 3.14. +- Avoid a segfault passing more arguments than placeholders if Python is built + with assertions enabled (:ticket:`#1791`). - `~psycopg2.errorcodes` map and `~psycopg2.errors` classes updated to PostgreSQL 18. - Drop support for Python 3.8. diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index 0bd36f49..e17e7172 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -342,7 +342,7 @@ _psyco_curs_merge_query_args(cursorObject *self, if (PyObject_HasAttrString(arg, "args")) { PyObject *args = PyObject_GetAttrString(arg, "args"); PyObject *str = PySequence_GetItem(args, 0); - const char *s = Bytes_AS_STRING(str); + const char *s = PyUnicode_AsUTF8(str); Dprintf("curs_execute: -> %s", s); diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 5de989ed..deaa99e1 100755 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -139,6 +139,11 @@ class CursorTests(ConnectingTestCase): self.assertRaises(psycopg2.ProgrammingError, cur.mogrify, "select %(foo, %(bar)", {'foo': 1, 'bar': 2}) + def test_bad_params_number(self): + cur = self.conn.cursor() + self.assertRaises(IndexError, cur.execute, "select %s, %s", [1]) + self.assertRaises(TypeError, cur.execute, "select %s", [1, 2]) + def test_cast(self): curs = self.conn.cursor()