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

gh-127182: Fix io.StringIO.__setstate__ crash when None is the first value #127219

Merged
merged 3 commits into from
Nov 25, 2024

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Nov 24, 2024

Change:

  • We now check for explicit None value in __setstate__, it cannot be recieved normally, because __getstate__ never returns None, but we should not let a simple direct call to crash, when the fix is rather simple
  • Why does it happen in the first place? Because we do the input validation with __init__ method:
    if (_io_StringIO___init__((PyObject *)self, initarg, NULL) < 0) {
  • But, None is an allowed input value, see
    if (value && value != Py_None && !PyUnicode_Check(value)) {
  • We did not check for None in __setstate__
  • NULL cannot be passed to __setstate__, because it requires a tuple input

self->string_size = bufsize;
}
else {
assert(item == Py_None);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be worth it to check item value at runtime, and fail with an assertion if it's not None.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that it is, because __init__ call above checks that. I don't think that it would be possible to trigger this error here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, it makes sense. I didn't see the _io_StringIO___init__() call.

Lib/test/test_io.py Outdated Show resolved Hide resolved
Co-authored-by: Victor Stinner <vstinner@python.org>
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

self->string_size = bufsize;
}
else {
assert(item == Py_None);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, it makes sense. I didn't see the _io_StringIO___init__() call.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

self->string_size = bufsize;
}
else {
assert(item == Py_None);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a side note, should we encourage using Py_Is instead of ==?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, Py_IsNone() can be used ;-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to keep it as == Py_None, because other related parts do the same thing: if (value && value != Py_None && !PyUnicode_Check(value)) in __init__, for example.

@sobolevn sobolevn merged commit a2ee899 into python:main Nov 25, 2024
44 checks passed
@miss-islington-app
Copy link

Thanks @sobolevn for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 25, 2024
…the first value (pythonGH-127219)

(cherry picked from commit a2ee899)

Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Victor Stinner <vstinner@python.org>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 25, 2024
…the first value (pythonGH-127219)

(cherry picked from commit a2ee899)

Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Victor Stinner <vstinner@python.org>
@bedevere-app
Copy link

bedevere-app bot commented Nov 25, 2024

GH-127262 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Nov 25, 2024
@bedevere-app
Copy link

bedevere-app bot commented Nov 25, 2024

GH-127263 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Nov 25, 2024
sobolevn added a commit that referenced this pull request Nov 25, 2024
… the first value (GH-127219) (#127263)

gh-127182: Fix `io.StringIO.__setstate__` crash when `None` is the first value (GH-127219)
(cherry picked from commit a2ee899)

Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Victor Stinner <vstinner@python.org>
sobolevn added a commit that referenced this pull request Nov 25, 2024
… the first value (GH-127219) (#127262)

gh-127182: Fix `io.StringIO.__setstate__` crash when `None` is the first value (GH-127219)
(cherry picked from commit a2ee899)

Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants