Skip to content

internal get_terminal_size() results in buffer overflow #127

@nexussapphire

Description

@nexussapphire

System

style50 version: 2.10.4
shell: bash -- version 5.3.0-release
Terminal: Kitty -- 0.43.1
Python: 3.14.2

OS: Fedora Linux 43
KDE Plasma Version: 6.5.4 (Wayland)
kernel version: 6.17.12-300.fc43.x86_64 (64-bit)

Error message

Traceback (most recent call last):
  File "/home/lee/.local/bin/style50", line 5, in <module>
    from style50.__main__ import main
  File "/home/lee/.local/lib/python3.14/site-packages/style50/__init__.py", line 17, in <module>
    from ._api import Style50, StyleCheck, Error
  File "/home/lee/.local/lib/python3.14/site-packages/style50/_api.py", line 52, in <module>
    COLUMNS, LINES = get_terminal_size()
                     ~~~~~~~~~~~~~~~~~^^
  File "/home/lee/.local/lib/python3.14/site-packages/style50/_api.py", line 39, in get_terminal_size
    data = fcntl.ioctl(stream.fileno(), TIOCGWINSZ, b"\x00\x00\00\x00")
SystemError: buffer overflow

bandaid

In _api.py I changed the internal get_terminal_size() to os.get_terminal_size().
This fixed my problem but is likely not the intended solution.

def get_terminal_size(fallback=(79, 24)):
    """
    Return tuple containing columns and rows of controlling terminal, trying harder
    than shutil.get_terminal_size to find a tty before returning fallback.

    Theoretically, stdout, stderr, and stdin could all be different ttys that could
    cause us to get the wrong measurements (instead of using the fallback) but the much more
    common case is that IO is piped.
    """
    for stream in [sys.__stdout__, sys.__stderr__, sys.__stdin__]:
        try:
            # Make WINSIZE call to terminal
            data = fcntl.ioctl(stream.fileno(), TIOCGWINSZ, b"\x-01\x00\00\x00")
        except OSError:
            pass
        else:
            # Unpack two shorts from ioctl call
            lines, columns = struct.unpack("hh", data)
            break
    else:
        columns, lines = fallback

    return columns, lines


# COLUMNS, LINES = get_terminal_size() 
COLUMNS, LINES = os.get_terminal_size()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions