-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect error handling for APIs that can raise exceptions #105375
Comments
This comment was marked as outdated.
This comment was marked as outdated.
Check for error after each call to PyUnicode_FromStringAndSize().
…pythonGH-105412) Check for error after each call to PyUnicode_FromStringAndSize(). (cherry picked from commit a24a780) Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
…pythonGH-105412) Check for error after each call to PyUnicode_FromStringAndSize(). (cherry picked from commit a24a780) Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Error handling was deferred in some cases, which could potentially lead to exceptions being overwritten.
There were many "init" functions of C extensions calling all C functions without checking for exceptions, and only checking for exceptions once at the end. Most of them have been fixed with better error checking, but not all of them yet. |
See issue #105374 about C API being error prone: when it's unclear if passing NULL was done on purpose or by mistake |
See also capi-workgroup/problems#47 |
Thank you, @erlend-aasland. These are minor errors, but it is nice to have the code cleaner. Although you introduced new bugs in #105475 and #105611 related to clearing non-initialized references. |
Thanks for double-checking, Serhiy. I really appreciate that. I will address your remarks. |
Refs #105673 |
…fields PyObject_GC_New() only initialises the object header. All other fields must be explicitly initialised to prevent manipulation of uninitialised fields in dealloc. Align initialisation order with the layout of the object structs.
…#105686) All fields must be explicitly initialised to prevent manipulation of uninitialised fields in dealloc. Align initialisation order with the layout of the object structs.
…fields (pythonGH-105686) All fields must be explicitly initialised to prevent manipulation of uninitialised fields in dealloc. Align initialisation order with the layout of the object structs. (cherry picked from commit ca3cc4b) Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
…Object fields (python#105686) All fields must be explicitly initialised to prevent manipulation of uninitialised fields in dealloc. Align initialisation order with the layout of the object structs. (cherry picked from commit ca3cc4b) Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
… fields (GH-105686) (#105710) All fields must be explicitly initialised to prevent manipulation of uninitialised fields in dealloc. Align initialisation order with the layout of the object structs. (cherry picked from commit ca3cc4b) Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
* main: pythongh-105540: Fix code generator tests (python#105707) pythongh-105375: Explicitly initialise all {Pickler,Unpickler}Object fields (python#105686) pythongh-105331: Change `asyncio.sleep` to raise ``ValueError` for nan (python#105641) Remove support for legacy bytecode instructions (python#105705)
… fields (#105686) (#105711) * [3.11] gh-105375: Explicitly initialise all {Pickler,Unpickler}Object fields (#105686) All fields must be explicitly initialised to prevent manipulation of uninitialised fields in dealloc. Align initialisation order with the layout of the object structs. (cherry picked from commit ca3cc4b) Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
…5667) Prevent exceptions from possibly being overwritten in case of multiple failures.
…Stream() (python#105667) Prevent exceptions from possibly being overwritten in case of multiple failures. (cherry picked from commit 217589d) Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
…Stream() (python#105667) Prevent exceptions from possibly being overwritten in case of multiple failures. (cherry picked from commit 217589d) Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
On lookup failure we would previously assume that binary addresses are valid unicode when constructing exception messages. With python/cpython#105375 which appeared e.g., in python-3.11.9 this starts to cause failures as invalid unicode is now more consistently rejected; e.g., in the test `pysubnettree.lookup` we construct a binary address `1.3.3.255` which corresponds to `b'\x01\x03\x03\xff'` which is not valid unicode. With this patch we set messages for `KeyError` from a `bytes` object instead of a `str`, so that user's see e.g., KeyError: b'1:3:3::3' instead of the previous KeyError: '1:3:3::3' This should still provide all the information necessary while working with our interface which allows both `str` and `bytes` inputs. The changes to `SubnetTree_wrap.cc` are generated automatically with swig-3.0.12.
On lookup failure we would previously assume that binary addresses are valid unicode when constructing exception messages. With python/cpython#105375 which appeared e.g., in python-3.11.9 this starts to cause failures as invalid unicode is now more consistently rejected; e.g., in the test `pysubnettree.lookup` we construct a binary address `1.3.3.255` which corresponds to `b'\x01\x03\x03\xff'` which is not valid unicode. With this patch we set messages for `KeyError` from a `bytes` object instead of a `str`, so that user's see e.g., KeyError: b'1:3:3::3' instead of the previous KeyError: '1:3:3::3' This should still provide all the information necessary while working with our interface which allows both `str` and `bytes` inputs. The changes to `SubnetTree_wrap.cc` are generated automatically with swig-3.0.12.
Also fix trace-summary python backtrace. Intermittent crash: zeek/zeek@8c337bd threading/MsgThread: Decouple IO source and thread A MsgThread acting as an IO source itself can result in the scenario where the threading manager's heartbeat timer deletes a terminated MsgThread instance, but at the same time this instance is in the list of ready IO sources as determined by the IO loop in the current iteration. trace-summary backtrace: zeek/pysubnettree#38 Fix extension for stricter unicode validation This fixes our extension module for python/cpython#105375 which made unicode validation stricter. Reported by: Arne Welzel (crash), ogogon (backtrace)
On lookup failure we would previously assume that binary addresses are valid unicode when constructing exception messages. With python/cpython#105375 which appeared e.g., in python-3.11.9 this starts to cause failures as invalid unicode is now more consistently rejected; e.g., in the test `pysubnettree.lookup` we construct a binary address `1.3.3.255` which corresponds to `b'\x01\x03\x03\xff'` which is not valid unicode. With this patch we set messages for `KeyError` from a `bytes` object instead of a `str`, so that user's see e.g., KeyError: b'1:3:3::3' instead of the previous KeyError: '1:3:3::3' This should still provide all the information necessary while working with our interface which allows both `str` and `bytes` inputs. The changes to `SubnetTree_wrap.cc` are generated automatically with swig-3.0.12. (cherry picked from commit d61edb0)
For example, in the collation callback two
str
objects (string1
andstring2
) are created usingPyUnicode_FromStringAndSize
. Error handling should happen directly after each call toPyUnicode_FromStringAndSize
:cpython/Modules/_sqlite/connection.c
Lines 1870 to 1875 in 0cb6b9b
Other cases where error handling is not done immediately after the API has been used:
zoneinfo
module #105586_testcapi/heaptype.c
#105608I might have missed some; I did not do a complete audit yet.
Linked PRs
zoneinfo
module #105586_testcapi/heaptype.c
#105608zoneinfo
module (GH-105586) #105612zoneinfo
module (GH-105586) #105613_testcapi/heaptype.c
(GH-105608) #105615The text was updated successfully, but these errors were encountered: