From a24a780d937109a0982d807473ae410cc75b0e3b Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 7 Jun 2023 13:10:28 +0200 Subject: [PATCH] gh-105375: Improve error handling in sqlite3 collation callback (#105412) Check for error after each call to PyUnicode_FromStringAndSize(). --- .../2023-06-07-00-09-52.gh-issue-105375.Y_9D4n.rst | 2 ++ Modules/_sqlite/connection.c | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-06-07-00-09-52.gh-issue-105375.Y_9D4n.rst 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 00000000000000..ec10d63822c203 --- /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 ` callback. diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 5c57a4101c4a69..82d23c2c30b798 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;