Fix C-API exception handling issues (lock OOM, signed char -1, exception preservation)#257
Open
dynapx wants to merge 1 commit into
Open
Fix C-API exception handling issues (lock OOM, signed char -1, exception preservation)#257dynapx wants to merge 1 commit into
dynapx wants to merge 1 commit into
Conversation
- Add PyErr_SetString for PyThread_allocate_lock failure (ffi_init_once: lock allocation failure does not set exception) - Add PyErr_NoMemory for ctypedescr_new OOM (new_struct_or_union_type: ctypedescr_new does not set exception on OOM) - Fix signed char -1 false negative detection (convert_from_object: -1 is valid signed char value, check PyErr_Occurred) - Preserve exceptions in _convert_to_char16_t/_convert_to_char32_t (avoid overwriting existing exception from _my_PyUnicode_AsSingleChar*)
Contributor
|
Please move to draft status until the issue is discussed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Title: Fix C-API exception handling issues (lock OOM, signed char -1)
Description
Hi maintainers,
This PR is a follow-up to #256 (which fixed unchecked
PyUnicode_AsUTF8returns). It addresses additional C-API exception handling issues identified during the same robustness audit of the_cffi_backendC extension.1.
PyThread_allocate_lockfailure does not set exceptionLocation:
ffi_obj.c:ffi_init_oncePyThread_allocate_lock()returnsNULLon failure but does not set a Python exception. The code currently returnsNULLwithout setting an exception, leading toSystemError.Fix: Add
PyErr_SetString()with a descriptive error message before returning.2.
ctypedescr_newOOM does not set exceptionLocation:
_cffi_backend.c:new_struct_or_union_typectypedescr_new()returnsNULLon OOM but does not set an exception. The caller returnsNULLwithout setting an exception.Fix: Add
PyErr_NoMemory().3. Signed char
-1false negative detectionLocation:
_cffi_backend.c:convert_from_object_convert_to_char(init)returns-1on error, but-1is also a valid signed char value (representing0xFF). The current code treats both as error, causing valid\xFFinput to fail without setting exception.Fix: Check
PyErr_Occurred()to distinguish between error and valid-1.Impact
These issues can cause:
SystemErrorwhenPyThread_allocate_lockfailsSystemErrorwhenctypedescr_newhits OOM\xFFbyte values