Skip to content

Conversation

@vlttnv
Copy link

@vlttnv vlttnv commented Nov 5, 2025

Fixes a SystemError: buffer overflow that occurs when the dashboard tries to get the terminal size using fcntl.ioctl() with TIOCGWINSZ.

Problem

The current implementation allocates a 4-byte buffer for the TIOCGWINSZ ioctl call:

raw = fcntl.ioctl(fd, termios.TIOCGWINSZ, ' ' * 4)

However, TIOCGWINSZ returns a complete struct winsize which is 8 bytes, not 4 bytes. According to the man page, the structure contains:

  • ws_row (2 bytes) - terminal rows
  • ws_col (2 bytes) - terminal columns
  • ws_xpixel (2 bytes) - horizontal pixels
  • ws_ypixel (2 bytes) - vertical pixels

When the ioctl attempts to write 8 bytes into a 4-byte buffer, Python raises a buffer overflow error.

Solution

Changed the buffer size from 4 bytes to 8 bytes to accommodate the full struct winsize:

raw = fcntl.ioctl(fd, termios.TIOCGWINSZ, ' ' * 8)

The code still only unpacks the first two shorts (height and width) as intended, but now provides adequate buffer space for the entire structure.

Also, we catch SystemError in the except block to fallback to the hardcoded values.

Valentin Tunev and others added 2 commits November 5, 2025 22:01
Use an 8-byte buffer, unpack the first 4 bytes for rows/cols, and
include SystemError in the except clause to cover additional terminal
errors.
@cyrus-and cyrus-and merged commit c429e14 into cyrus-and:master Nov 6, 2025
@cyrus-and
Copy link
Owner

Great catch, thanks! Although a crash never occurred to me...

Polished a bit.

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.

2 participants