Skip to content
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

Open
5 of 18 tasks
erlend-aasland opened this issue Jun 6, 2023 · 6 comments · Fixed by #105412, #105475, #105494, #105590 or #105592
Open
5 of 18 tasks

Incorrect error handling for APIs that can raise exceptions #105375

erlend-aasland opened this issue Jun 6, 2023 · 6 comments · Fixed by #105412, #105475, #105494, #105590 or #105592
Assignees
Labels
3.11 bug and security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes topic-C-API type-bug An unexpected behavior, bug, or error

Comments

@erlend-aasland
Copy link
Contributor

erlend-aasland commented Jun 6, 2023

For example, in the collation callback two str objects (string1 and string2) are created using PyUnicode_FromStringAndSize. Error handling should happen directly after each call to PyUnicode_FromStringAndSize:

string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
if (!string1 || !string2) {
goto finally; /* failed to allocate strings */
}

Other cases where error handling is not done immediately after the API has been used:

I might have missed some; I did not do a complete audit yet.

Linked PRs

@erlend-aasland erlend-aasland added the type-bug An unexpected behavior, bug, or error label Jun 6, 2023
@erlend-aasland erlend-aasland self-assigned this Jun 6, 2023
@erlend-aasland

This comment was marked as outdated.

erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue Jun 6, 2023
Check for error after each call to PyUnicode_FromStringAndSize().
erlend-aasland added a commit that referenced this issue Jun 7, 2023
)

Check for error after each call to PyUnicode_FromStringAndSize().
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 7, 2023
…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>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 7, 2023
…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>
@erlend-aasland erlend-aasland changed the title Incorrect error handling in sqlite3 collation callback Missing error handling for APIs that can raise exceptions Jun 7, 2023
@erlend-aasland erlend-aasland changed the title Missing error handling for APIs that can raise exceptions Incorrect error handling for APIs that can raise exceptions Jun 7, 2023
erlend-aasland added a commit that referenced this issue Jun 7, 2023
GH-105412) (#105440)

Check for error after each call to PyUnicode_FromStringAndSize().
(cherry picked from commit a24a780)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
@erlend-aasland erlend-aasland reopened this Jun 7, 2023
erlend-aasland added a commit that referenced this issue Jun 7, 2023
GH-105412) (#105441)

Check for error after each call to PyUnicode_FromStringAndSize().
(cherry picked from commit a24a780)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue Jun 7, 2023
Error handling was deferred in some cases, which could potentially lead
to exceptions being overwritten.
erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue Jun 8, 2023
@vstinner
Copy link
Member

vstinner commented Jun 9, 2023

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.

@vstinner
Copy link
Member

vstinner commented Jun 9, 2023

See issue #105374 about C API being error prone: when it's unclear if passing NULL was done on purpose or by mistake

@vstinner
Copy link
Member

vstinner commented Jun 9, 2023

See also capi-workgroup/problems#47

erlend-aasland added a commit that referenced this issue Jun 9, 2023
(cherry picked from commit eede1d2)

Bail immediately if an exception is set, to prevent exceptions from
being overwritten.
@erlend-aasland erlend-aasland linked a pull request Jun 9, 2023 that will close this issue
erlend-aasland added a commit that referenced this issue Jun 9, 2023
(cherry picked from commit eede1d2)

Bail immediately if an exception is set, to prevent exceptions from
being overwritten.
erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue Jun 9, 2023
Improve error handling so init bails on the first exception.
erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue Jun 9, 2023
Fix a bug where an exception could end up being overwritten.
erlend-aasland added a commit that referenced this issue Jun 9, 2023
Fix a bug where an IndexError could end up being overwritten.
(cherry picked from commit f668f73)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
erlend-aasland added a commit that referenced this issue Jun 9, 2023
…105600)

Fix bugs where exceptions could end up being overwritten.
(cherry picked from commit 00b599a)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
erlend-aasland added a commit that referenced this issue Jun 9, 2023
…105601)

Fix bugs where exceptions could end up being overwritten.
(cherry picked from commit 00b599a)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue Jun 9, 2023
Bail on first error in heapctypesubclasswithfinalizer_finalize()
erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue Jun 9, 2023
Bail on first error to prevent exceptions from possibly being
overwritten.
erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue Jun 9, 2023
Bail on first error to prevent exceptions from possibly being
overwritten.
erlend-aasland added a commit that referenced this issue Jun 9, 2023
Fix bugs where exceptions could end up being overwritten
because of deferred error handling.

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 9, 2023
…H-105586)

Fix bugs where exceptions could end up being overwritten
because of deferred error handling.

(cherry picked from commit 33c92c4)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 9, 2023
…H-105586)

Fix bugs where exceptions could end up being overwritten
because of deferred error handling.

(cherry picked from commit 33c92c4)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
erlend-aasland added a commit that referenced this issue Jun 9, 2023
Bail on first error in heapctypesubclasswithfinalizer_finalize()
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 9, 2023
…honGH-105608)

Bail on first error in heapctypesubclasswithfinalizer_finalize()
(cherry picked from commit d636d7dfe714e7168b342c7ea5f9f9d3b3569ed0)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
erlend-aasland added a commit that referenced this issue Jun 9, 2023
) (#105613)

Fix bugs where exceptions could end up being overwritten
because of deferred error handling.

(cherry picked from commit 33c92c4)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
erlend-aasland added a commit that referenced this issue Jun 9, 2023
…-105608) (#105615)

Bail on first error in heapctypesubclasswithfinalizer_finalize()
(cherry picked from commit d636d7d)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
erlend-aasland added a commit that referenced this issue Jun 9, 2023
) (#105612)

Fix bugs where exceptions could end up being overwritten
because of deferred error handling.

(cherry picked from commit 33c92c4)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
@sobolevn
Copy link
Member

Wow, it was a productive night! Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment