Skip to content

Correctly handle errors in PyErr_Set*() functions #107915

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

Closed
serhiy-storchaka opened this issue Aug 13, 2023 · 0 comments · Fixed by #108134
Closed

Correctly handle errors in PyErr_Set*() functions #107915

serhiy-storchaka opened this issue Aug 13, 2023 · 0 comments · Fixed by #108134
Assignees
Labels
3.11 only security fixes 3.12 only security fixes 3.13 bugs and security fixes topic-C-API type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Aug 13, 2023

C API functions PyErr_SetFromErrnoWithFilename(), PyErr_SetExcFromWindowsErrWithFilename(), PyErr_SetFromWindowsErrWithFilename() , _PyErr_SetString() and _PyErr_FormatV() convert some of their arguments from C string to Python string, but do not check for error. If it fails the behavior is undefined -- it can be ignoring the new error and using None or other default value instead of the string, it can be crash.

Linked PRs

@serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error 3.11 only security fixes topic-C-API 3.12 only security fixes 3.13 bugs and security fixes labels Aug 13, 2023
@serhiy-storchaka serhiy-storchaka self-assigned this Aug 13, 2023
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 13, 2023
…Err_Format()

Such C API functions as PyErr_SetString(), PyErr_Format(),
PyErr_SetFromErrnoWithFilename() and many others no longer crash or
ignore errors if it failed to format the error message or decode the
filename. Instead, they keep a corresponding error.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 13, 2023
…Err_Format()

Such C API functions as PyErr_SetString(), PyErr_Format(),
PyErr_SetFromErrnoWithFilename() and many others no longer crash or
ignore errors if it failed to format the error message or decode the
filename. Instead, they keep a corresponding error.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 13, 2023
…Err_Format()

Such C API functions as PyErr_SetString(), PyErr_Format(),
PyErr_SetFromErrnoWithFilename() and many others no longer crash or
ignore errors if it failed to format the error message or decode the
filename. Instead, they keep a corresponding error.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 13, 2023
…Err_Format()

Such C API functions as PyErr_SetString(), PyErr_Format(),
PyErr_SetFromErrnoWithFilename() and many others no longer crash or
ignore errors if it failed to format the error message or decode the
filename. Instead, they keep a corresponding error.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 13, 2023
…Err_Format()

Such C API functions as PyErr_SetString(), PyErr_Format(),
PyErr_SetFromErrnoWithFilename() and many others no longer crash or
ignore errors if it failed to format the error message or decode the
filename. Instead, they keep a corresponding error.
serhiy-storchaka added a commit that referenced this issue Aug 19, 2023
…rmat() (GH-107918)

Such C API functions as PyErr_SetString(), PyErr_Format(),
PyErr_SetFromErrnoWithFilename() and many others no longer crash or
ignore errors if it failed to format the error message or decode the
filename. Instead, they keep a corresponding error.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Aug 19, 2023
…Err_Format() (pythonGH-107918)

Such C API functions as PyErr_SetString(), PyErr_Format(),
PyErr_SetFromErrnoWithFilename() and many others no longer crash or
ignore errors if it failed to format the error message or decode the
filename. Instead, they keep a corresponding error.
(cherry picked from commit 633ea21)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 19, 2023
… and PyErr_Format() (pythonGH-107918)

Such C API functions as PyErr_SetString(), PyErr_Format(),
PyErr_SetFromErrnoWithFilename() and many others no longer crash or
ignore errors if it failed to format the error message or decode the
filename. Instead, they keep a corresponding error..
(cherry picked from commit 633ea21)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this issue Aug 19, 2023
…yErr_Format() (GH-107918) (GH-108135)

Such C API functions as PyErr_SetString(), PyErr_Format(),
PyErr_SetFromErrnoWithFilename() and many others no longer crash or
ignore errors if it failed to format the error message or decode the
filename. Instead, they keep a corresponding error.
(cherry picked from commit 633ea21)
Yhg1s pushed a commit that referenced this issue Aug 20, 2023
…yErr_Format() (GH-107918) (#108134)

* gh-107915: Handle errors in C API functions PyErr_Set*() and PyErr_Format() (GH-107918)

Such C API functions as PyErr_SetString(), PyErr_Format(),
PyErr_SetFromErrnoWithFilename() and many others no longer crash or
ignore errors if it failed to format the error message or decode the
filename. Instead, they keep a corresponding error.
(cherry picked from commit 633ea21)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>

* Define PY_SSIZE_T_CLEAN.

---------

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 only security fixes 3.13 bugs and security fixes topic-C-API type-bug An unexpected behavior, bug, or error
Projects
None yet
1 participant