diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-08-06 17:59:51 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-06 17:59:51 (GMT) |
commit | 2b1e713f877102bbca299f0f5d7db969d78db49f (patch) | |
tree | 48003eccc512d0307da2bab7fba2fb4b16333cc2 /Modules | |
parent | a5d99632766b458b42f327e8bd0f82b0345c9a63 (diff) | |
download | cpython-2b1e713f877102bbca299f0f5d7db969d78db49f.zip cpython-2b1e713f877102bbca299f0f5d7db969d78db49f.tar.gz cpython-2b1e713f877102bbca299f0f5d7db969d78db49f.tar.bz2 |
bpo-44822: Don't truncate `str`s with embedded NULL chars returned by `sqlite3` UDF callbacks (GH-27588)
(cherry picked from commit 8f010dc920e1f6dc6a357e7cc1460a7a567c05c6)
Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_sqlite/connection.c | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 9c05a15..f060ef1 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -549,10 +549,17 @@ _pysqlite_set_result(sqlite3_context* context, PyObject* py_val) } else if (PyFloat_Check(py_val)) { sqlite3_result_double(context, PyFloat_AsDouble(py_val)); } else if (PyUnicode_Check(py_val)) { - const char *str = PyUnicode_AsUTF8(py_val); - if (str == NULL) + Py_ssize_t sz; + const char *str = PyUnicode_AsUTF8AndSize(py_val, &sz); + if (str == NULL) { return -1; - sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT); + } + if (sz > INT_MAX) { + PyErr_SetString(PyExc_OverflowError, + "string is longer than INT_MAX bytes"); + return -1; + } + sqlite3_result_text(context, str, (int)sz, SQLITE_TRANSIENT); } else if (PyObject_CheckBuffer(py_val)) { Py_buffer view; if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) { |