Skip to content

Avoid division by zero on small windows#214

Open
edgar-bonet wants to merge 2 commits intotenox7:masterfrom
edgar-bonet:no-fpe
Open

Avoid division by zero on small windows#214
edgar-bonet wants to merge 2 commits intotenox7:masterfrom
edgar-bonet:no-fpe

Conversation

@edgar-bonet
Copy link
Collaborator

If the window is too small when the program starts, ttyplot crashes on a division by zero (“Floating-point exception”). This is because plotwidth never gets set to a non-zero value, and handle_value() performs a modulo operation with plotwidth as the denominator.

This pull request fixes the crash by initializing plotwidth with the minimum value it can sensibly have.

If the window is too small when the program starts, plotwidth stays at
zero. As soon as data arrives, handle_value() computes the appropriate
array index to store it, as

    n = (n + 1) % plotwidth;

The division by zero makes the program crash with SIGFPE (Floating-point
exception).

Avoid the crash by initializing plotwidth to the smallest value it can
sensibly have.
@edgar-bonet edgar-bonet requested review from hartwork and tenox7 and removed request for hartwork March 8, 2026 14:02
Copy link
Collaborator

@hartwork hartwork left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@edgar-bonet nice one! I confirm crasher COLUMNS=1 ./ttyplot <<<"1 2 3 4" fixed.

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