Skip to content

TextIOWrapper.reconfigure() crashes if encoding is not string or None #111942

Description

@serhiy-storchaka

Crash report

What happened?

Unlike to TextIOWrapper constructor, TextIOWrapper.reconfigure() does not check that the encoding argument is actually None or string. If it is not None, it calls _PyUnicode_EqualToASCIIString that only works with strings and crashes in debug build. There may be other similar errors in other arguments.

>>> import sys
>>> sys.stdout.reconfigure(encoding=42)
Objects/unicodeobject.c:552: _PyUnicode_CheckConsistency: Assertion failed: PyType_HasFeature((Py_TYPE(((PyObject*)((op))))), ((1UL << 28)))
Enable tracemalloc to get the memory block allocation traceback

object address  : 0x55a2e3147ca8
object refcount : 4294967295
object type     : 0x55a2e3119f00
object type name: int
object repr     : 42

Fatal Python error: _PyObject_AssertFailed: _PyObject_AssertFailed
Python runtime state: initialized

Current thread 0x00007fc99ee43740 (most recent call first):
  File "<stdin>-1", line 1 in <module>
Aborted (core dumped)

CPython versions tested on:

3.10, 3.11, 3.12

Operating systems tested on:

No response

Output from running 'python -VV' on the command line:

No response

Linked PRs

Activity

  1. added 2 commits that reference this issue on Nov 10, 2023
  2. added 2 commits that reference this issue on Nov 14, 2023
  3. added 3 commits that reference this issue on Nov 14, 2023
  4. serhiy-storchaka commented on Nov 14, 2023

    @serhiy-storchaka
    MemberAuthor

    Weird. The address sanitizer tests were passed in main and 3.12, but failed in 3.11. And I now can reproduce the failure locally in main.

    The newly added tests exposed a bug in the TextIOWrapper constructor when pass a non-encodable "errors" argument.

  5. added 2 commits that reference this issue on Nov 14, 2023
  6. added a commit that references this issue on Nov 14, 2023
  7. added 2 commits that reference this issue on Nov 14, 2023
  8. added 3 commits that reference this issue on Nov 15, 2023
  9. vstinner commented on Nov 15, 2023

    @vstinner
    Member

    Nice thanks, thanks.

  10. added 2 commits that reference this issue on Feb 11, 2024
  11. added 2 commits that reference this issue on Sep 2, 2024
  12. encukou commented on Nov 7, 2024

    @encukou
    Member

    There's an issue with the 3.11 backport, in textiowrapper_change_encoding. Main had:

    +    Py_INCREF(errors);
     ...
         Py_SETREF(self->encoding, encoding);
    -    Py_SETREF(self->errors, Py_NewRef(errors));
    +    Py_SETREF(self->errors, errors);

    but in 3.11 this is:

    +    Py_INCREF(errors);
    ...
         Py_INCREF(errors);
         Py_SETREF(self->encoding, encoding);
         Py_SETREF(self->errors, errors);

    i.e. there's an extra incref, but it looks -- at least to Git -- like the change that removes Py_NewRef was already applied.

    This was not caught because test_io refleaks tests were ineffective on 3.11.

    I'm preparing a fix.

  13. added a commit that references this issue on Nov 7, 2024
  14. added a commit that references this issue on Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

topic-IOtype-crashA hard crash of the interpreter, possibly with a core dump

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions