[Python-checkins] gh-105375: Improve error handling in sqlite3 collation callback (#105412)

erlend-aasland webhook-mailer at python.org
Wed Jun 7 07:10:36 EDT 2023


https://github.com/python/cpython/commit/a24a780d937109a0982d807473ae410cc75b0e3b
commit: a24a780d937109a0982d807473ae410cc75b0e3b
branch: main
author: Erlend E. Aasland <erlend.aasland at protonmail.com>
committer: erlend-aasland <erlend.aasland at protonmail.com>
date: 2023-06-07T13:10:28+02:00
summary:

gh-105375: Improve error handling in sqlite3 collation callback (#105412)

Check for error after each call to PyUnicode_FromStringAndSize().

files:
A Misc/NEWS.d/next/Library/2023-06-07-00-09-52.gh-issue-105375.Y_9D4n.rst
M Modules/_sqlite/connection.c

diff --git a/Misc/NEWS.d/next/Library/2023-06-07-00-09-52.gh-issue-105375.Y_9D4n.rst b/Misc/NEWS.d/next/Library/2023-06-07-00-09-52.gh-issue-105375.Y_9D4n.rst
new file mode 100644
index 0000000000000..ec10d63822c20
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-06-07-00-09-52.gh-issue-105375.Y_9D4n.rst
@@ -0,0 +1,2 @@
+Fix a bug in :mod:`sqlite3` where an exception could be overwritten in the
+:meth:`collation <sqlite3.Connection.create_collation>` callback.
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 5c57a4101c4a6..82d23c2c30b79 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -1868,10 +1868,12 @@ collation_callback(void *context, int text1_length, const void *text1_data,
     }
 
     string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
+    if (string1 == NULL) {
+        goto finally;
+    }
     string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
-
-    if (!string1 || !string2) {
-        goto finally; /* failed to allocate strings */
+    if (string2 == NULL) {
+        goto finally;
     }
 
     callback_context *ctx = (callback_context *)context;



More information about the Python-checkins mailing list