From 7e9dadda9efe459d7b9679b3282cfb99f3325ce8 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 10 Feb 2021 21:49:28 +0100 Subject: [PATCH 1/4] First argument of sqlite3.Connection.backup is mandatory --- Modules/_sqlite/clinic/connection.c.h | 29 +++++++++------------------ Modules/_sqlite/connection.c | 4 ++-- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/Modules/_sqlite/clinic/connection.c.h b/Modules/_sqlite/clinic/connection.c.h index 01b8e37a957fc2..f231ecc2ae78be 100644 --- a/Modules/_sqlite/clinic/connection.c.h +++ b/Modules/_sqlite/clinic/connection.c.h @@ -519,8 +519,8 @@ pysqlite_connection_iterdump(pysqlite_Connection *self, PyObject *Py_UNUSED(igno } PyDoc_STRVAR(pysqlite_connection_backup__doc__, -"backup($self, /, target=, *, pages=-1, progress=None,\n" -" name=\'main\', sleep=0.25)\n" +"backup($self, /, target, *, pages=-1, progress=None, name=\'main\',\n" +" sleep=0.25)\n" "--\n" "\n" "Makes a backup of the database. Non-standard."); @@ -541,31 +541,22 @@ pysqlite_connection_backup(pysqlite_Connection *self, PyObject *const *args, Py_ static const char * const _keywords[] = {"target", "pages", "progress", "name", "sleep", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "backup", 0}; PyObject *argsbuf[5]; - Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; - pysqlite_Connection *target = NULL; + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; + pysqlite_Connection *target; int pages = -1; PyObject *progress = Py_None; const char *name = "main"; double sleep = 0.25; - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf); + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); if (!args) { goto exit; } - if (!noptargs) { - goto skip_optional_pos; - } - if (args[0]) { - if (!PyObject_TypeCheck(args[0], pysqlite_ConnectionType)) { - _PyArg_BadArgument("backup", "argument 'target'", (pysqlite_ConnectionType)->tp_name, args[0]); - goto exit; - } - target = (pysqlite_Connection *)args[0]; - if (!--noptargs) { - goto skip_optional_pos; - } + if (!PyObject_TypeCheck(args[0], pysqlite_ConnectionType)) { + _PyArg_BadArgument("backup", "argument 'target'", (pysqlite_ConnectionType)->tp_name, args[0]); + goto exit; } -skip_optional_pos: + target = (pysqlite_Connection *)args[0]; if (!noptargs) { goto skip_optional_kwonly; } @@ -719,4 +710,4 @@ pysqlite_connection_exit(pysqlite_Connection *self, PyObject *const *args, Py_ss #ifndef PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF #define PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF #endif /* !defined(PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF) */ -/*[clinic end generated code: output=7cb13d491a5970aa input=a9049054013a1b77]*/ +/*[clinic end generated code: output=c1bf09db3bcd0105 input=a9049054013a1b77]*/ diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 370dc1a30e46a1..63fcb0055de2c9 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -1582,7 +1582,7 @@ pysqlite_connection_iterdump_impl(pysqlite_Connection *self) /*[clinic input] _sqlite3.Connection.backup as pysqlite_connection_backup - target: object(type='pysqlite_Connection *', subclass_of='pysqlite_ConnectionType') = NULL + target: object(type='pysqlite_Connection *', subclass_of='pysqlite_ConnectionType') * pages: int = -1 progress: object = None @@ -1597,7 +1597,7 @@ pysqlite_connection_backup_impl(pysqlite_Connection *self, pysqlite_Connection *target, int pages, PyObject *progress, const char *name, double sleep) -/*[clinic end generated code: output=306a3e6a38c36334 input=2f3497ea530144b1]*/ +/*[clinic end generated code: output=306a3e6a38c36334 input=30ae45fc420bfd3b]*/ { int rc; int callback_error = 0; From 6a3f3b4143273ebd7e7c527eb53a8875bf65df74 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 10 Feb 2021 23:27:21 +0100 Subject: [PATCH 2/4] Address review: add test --- Lib/sqlite3/test/backup.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/sqlite3/test/backup.py b/Lib/sqlite3/test/backup.py index ddff78c7607b27..cbe24df2e9c96c 100644 --- a/Lib/sqlite3/test/backup.py +++ b/Lib/sqlite3/test/backup.py @@ -17,9 +17,11 @@ def verify_backup(self, bckcx): self.assertEqual(result[0][0], 3) self.assertEqual(result[1][0], 4) - def test_bad_target_none(self): + def test_bad_target(self): with self.assertRaises(TypeError): self.cx.backup(None) + with self.assertRaises(TypeError): + self.cx.backup() def test_bad_target_filename(self): with self.assertRaises(TypeError): From da330e8b4b644c44cf816efd435ca42b00a47199 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 10 Feb 2021 23:29:55 +0100 Subject: [PATCH 3/4] Add NEWS --- .../next/Library/2021-02-10-23-29-50.bpo-40956.LcAbwG.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-02-10-23-29-50.bpo-40956.LcAbwG.rst diff --git a/Misc/NEWS.d/next/Library/2021-02-10-23-29-50.bpo-40956.LcAbwG.rst b/Misc/NEWS.d/next/Library/2021-02-10-23-29-50.bpo-40956.LcAbwG.rst new file mode 100644 index 00000000000000..4249af68cd0b2e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-02-10-23-29-50.bpo-40956.LcAbwG.rst @@ -0,0 +1,2 @@ +Fix segfault in ``sqlite3.Connection.backup`` if no argument was provided. +The regression was introduced by GH-23838. Patch by Erlend E. Aasland. From 105aa2cbb0dba14d7d2904264b3bf1ecb3691807 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Thu, 11 Feb 2021 00:38:25 +0200 Subject: [PATCH 4/4] use meth --- .../next/Library/2021-02-10-23-29-50.bpo-40956.LcAbwG.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2021-02-10-23-29-50.bpo-40956.LcAbwG.rst b/Misc/NEWS.d/next/Library/2021-02-10-23-29-50.bpo-40956.LcAbwG.rst index 4249af68cd0b2e..e81922c0315671 100644 --- a/Misc/NEWS.d/next/Library/2021-02-10-23-29-50.bpo-40956.LcAbwG.rst +++ b/Misc/NEWS.d/next/Library/2021-02-10-23-29-50.bpo-40956.LcAbwG.rst @@ -1,2 +1,3 @@ -Fix segfault in ``sqlite3.Connection.backup`` if no argument was provided. -The regression was introduced by GH-23838. Patch by Erlend E. Aasland. +Fix segfault in :meth:`sqlite3.Connection.backup` if no argument was +provided. The regression was introduced by GH-23838. Patch by +Erlend E. Aasland.