Skip to content

SystemError from termios.tcsetwinsizepassing NULL to PyLong_AsLong #146091

Description

@devdanzin

Crash report

What happened?

It's possible to raise SystemError from termios.tcsetwinsize by passing a sequence that raises on __getitem__ to it.

Automated diagnosis:

Bug: termios.tcsetwinsize passes NULL to PyLong_AsLong. PySequence_GetItem(winsz, 0) can return NULL (e.g., __getitem__ raises). The result is passed directly to PyLong_AsLong(NULL) which calls PyErr_BadInternalCall(), masking the real error with SystemError.

File: Modules/termios.c, lines 502-510

MRE:

import termios
import os

fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY)

class BadSequence:
    def __len__(self):
        return 2
    def __getitem__(self, idx):
        raise TypeError

termios.tcsetwinsize(fd, BadSequence())

Backtrace:

Traceback (most recent call last):
  File "/home/danzin/crashers/trigger_termios_null_pylong.py", line 23, in <module>
    termios.tcsetwinsize(fd, BadSequence())
    ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
SystemError: Objects/longobject.c:598: bad argument to internal function

Found using cpython-review-toolkit with Claude Opus 4.6, using the /cpython-review-toolkit:explore Modules/termios.c all deep command.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

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

Python 3.15.0a7+ (heads/main:e0f7c1097e1, Mar 17 2026, 18:10:52) [Clang 21.1.2 (2ubuntu6)]

Linked PRs

Activity

  1. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Mar 17, 2026
  2. added
    3.13only security fixes
    3.14bugs and security fixes
    3.15bugs and security fixes
    on Mar 18, 2026
  3. okiemute04 commented on Mar 18, 2026

    @okiemute04
    Contributor

    @devdanzin This is interesting. Let me work on this issue. I do understand the problem is that PySequence_GetItem can return NULL, which gets passed to PyLong_AsLong, causing a SystemError instead of the original exception. I'll add NULL checks and proper error propagation, and any suggestions that can help?

  4. added 3 commits that reference this issue on Mar 18, 2026
  5. added a commit that references this issue on Mar 19, 2026
  6. added a commit that references this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixes3.15bugs and security fixesextension-modulesC modules in the Modules dirtype-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