gh-127182: Fix io.StringIO.__setstate__ crash when None is the first value#127219
gh-127182: Fix io.StringIO.__setstate__ crash when None is the first value#127219sobolevn merged 3 commits intopython:mainfrom
io.StringIO.__setstate__ crash when None is the first value#127219Conversation
…the first value
| self->string_size = bufsize; | ||
| } | ||
| else { | ||
| assert(item == Py_None); |
There was a problem hiding this comment.
It may be worth it to check item value at runtime, and fail with an assertion if it's not None.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Oh right, it makes sense. I didn't see the _io_StringIO___init__() call.
Co-authored-by: Victor Stinner <vstinner@python.org>
| self->string_size = bufsize; | ||
| } | ||
| else { | ||
| assert(item == Py_None); |
There was a problem hiding this comment.
Oh right, it makes sense. I didn't see the _io_StringIO___init__() call.
| self->string_size = bufsize; | ||
| } | ||
| else { | ||
| assert(item == Py_None); |
There was a problem hiding this comment.
As a side note, should we encourage using Py_Is instead of ==?
There was a problem hiding this comment.
In this case, Py_IsNone() can be used ;-)
There was a problem hiding this comment.
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.
|
Thanks @sobolevn for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. |
…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>
…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>
|
GH-127262 is a backport of this pull request to the 3.13 branch. |
|
GH-127263 is a backport of this pull request to the 3.12 branch. |
…the first value (python#127219) Co-authored-by: Victor Stinner <vstinner@python.org>
Change:
Nonevalue in__setstate__, it cannot be recieved normally, because__getstate__never returnsNone, but we should not let a simple direct call to crash, when the fix is rather simple__init__method:cpython/Modules/_io/stringio.c
Line 898 in f7bb658
Noneis an allowed input value, seecpython/Modules/_io/stringio.c
Line 693 in f7bb658
Nonein__setstate__NULLcannot be passed to__setstate__, because it requires atupleinputStringIO.__setstate__#127182