This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [sqlite3] Fix sqlite3_value_blob() usage
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, erlendaasland, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2021-02-22 14:37 by erlendaasland, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 24674 merged erlendaasland, 2021-02-28 23:09
Messages (5)
msg387516 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-02-22 14:37
The sqlite3_value_*() API is almost identical to the sqlite3_column_*() API. sqlite3_value_bytes() should be called after we've converted the value using sqlite3_value_blob().

See also bpo-43249.
msg387541 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-02-22 20:13
Related:
If sqlite3_value_blob() returns NULL, we should check if sqlite3_errcode() equals SQLITE_NOMEM and raise MemoryError if it does.

If not, we should initialise cur_py_value to None, because as the PyBytes_FromStringAndSize docs says: "If v is NULL, the contents of the bytes object are uninitialized."
msg387719 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-02-26 11:22
> If sqlite3_value_blob() returns NULL, we should check if sqlite3_errcode() equals SQLITE_NOMEM and raise MemoryError if it does.

This also applies to sqlite3_value_text().

It also applies to sqlite3_value_bytes() if a conversion takes place. We don't need to care about that as long as we call sqlite3_value_bytes() after conversion.

Also, PyTuple_SetItem() errors are not checked.

I would also suggest using PyUnicode_FromStringAndSize() iso. PyUnicode_FromString() in case SQLITE_TEXT, to avoid the unneeded strlen(). After sqlite3_value_text() is called, we can just use sqlite3_value_bytes(), since the length is precomputed as a result of the conversion.

Berker, would you allow a PR to improve all these issues in _pysqlite_build_py_params(), or would you prefer them split up in multiple PR's?
msg391100 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2021-04-14 21:09
New changeset 5cb601f956886b32641f818b5da347cc86a43db2 by Erlend Egeberg Aasland in branch 'master':
bpo-43296: Handle sqlite3_value_blob() errors (GH-24674)
https://github.com/python/cpython/commit/5cb601f956886b32641f818b5da347cc86a43db2
msg391101 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-04-14 21:15
Thanks, Berker!

I'll add separate issues for sqlite3_value_text() and the missing PyTuple_SetItem() error checks.
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87462
2021-04-14 21:15:00erlendaaslandsetmessages: + msg391101
2021-04-14 21:09:41berker.peksagsetmessages: + msg391100
2021-04-14 21:09:30berker.peksagsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-02-28 23:09:01erlendaaslandsetkeywords: + patch
stage: patch review
pull_requests: + pull_request23459
2021-02-26 11:22:20erlendaaslandsetmessages: + msg387719
2021-02-26 09:46:13erlendaaslandsettitle: [sqlite3] sqlite3_value_bytes() should be called after sqlite3_value_blob() -> [sqlite3] Fix sqlite3_value_blob() usage
2021-02-22 20:13:50erlendaaslandsetmessages: + msg387541
2021-02-22 14:37:13erlendaaslandcreate