From 9c6c325f83348ee7215fb7a7aca3bc6316217056 Mon Sep 17 00:00:00 2001 From: Drew Smirnoff Date: Mon, 26 Oct 2026 17:59:16 +0400 Subject: [PATCH] fix: ingnore implausible terminal resize reports (#1887) Signed-off-by: drew --- screen.go | 15 +++++++++++++++ screen_test.go | 22 ++++++++++++++++++++++ tea.go | 4 ++++ 3 files changed, 41 insertions(+) diff --git a/screen.go b/screen.go index e799a542..d590b255 100644 --- a/screen.go +++ b/screen.go @@ -64,3 +64,18 @@ type ModeReportMsg struct { // Value is the mode value. Value ansi.ModeSetting } + +const ( + maxWindowDimension = 4096 + maxWindowCells = 1 << 20 +) + +func isPlausibleWindowSize(width, height int) bool { + if width < 0 || height < 0 { + return false + } + if width > maxWindowDimension || height > maxWindowDimension { + return false + } + return width*height <= maxWindowCells +} diff --git a/screen_test.go b/screen_test.go index c3fe24ab..3ba91536 100644 --- a/screen_test.go +++ b/screen_test.go @@ -205,3 +205,25 @@ func TestClearMsg(t *testing.T) { }) } } + +func TestIsPlausibleWindowSize(t *testing.T) { + tests := []struct { + w, h int + want bool + }{ + {80, 24, true}, + {0, 0, true}, + {500, 200, true}, + {-1, 24, false}, + {80, -1, false}, + {5000, 1, false}, + {23000, 23000, false}, + {29000, 29000, false}, + {2000, 2000, false}, + } + for _, tt := range tests { + if got := isPlausibleWindowSize(tt.w, tt.h); got != tt.want { + t.Errorf("isPlausibleWindowSize(%d, %d) = %v, want %v", tt.w, tt.h, got, tt.want) + } + } +} diff --git a/tea.go b/tea.go index 95ce4a8a..72bac7c4 100644 --- a/tea.go +++ b/tea.go @@ -776,6 +776,10 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) { case msg := <-p.msgs: msg = p.translateInputEvent(msg) + if size, ok := msg.(WindowSizeMsg); ok && !isPlausibleWindowSize(size.Width, size.Height) { + continue + } + // Filter messages. if p.filter != nil { msg = p.filter(model, msg)