From 0751b389321cde6db5528704fc3613910f30ccbb Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 24 Nov 2023 19:16:33 -0800 Subject: establish a fairly fundamental invariant This commit doesn't guarantee we'll always catch it. But if this invariant is violated, things can get quite difficult to debug. I found in the Lua Carousel fork that all the xpcalls I keep around were actively hindering my ability to notice this invariant being violated. --- source_text.lua | 6 ++++++ text.lua | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/source_text.lua b/source_text.lua index 8b34d52..f93e859 100644 --- a/source_text.lua +++ b/source_text.lua @@ -1035,6 +1035,12 @@ end function Text.redraw_all(State) --? print('clearing fragments') + -- Perform some early sanity checking here, in hopes that we correctly call + -- this whenever we change editor state. + if State.right <= State.left then + assert(false, ('Right margin %d must be to the right of the left margin %d'):format(State.right, State.left)) + end + State.line_cache = {} for i=1,#State.lines do State.line_cache[i] = {} diff --git a/text.lua b/text.lua index cd80464..db51ac4 100644 --- a/text.lua +++ b/text.lua @@ -973,6 +973,12 @@ end function Text.redraw_all(State) --? print('clearing fragments') + -- Perform some early sanity checking here, in hopes that we correctly call + -- this whenever we change editor state. + if State.right <= State.left then + assert(false, ('Right margin %d must be to the right of the left margin %d'):format(State.right, State.left)) + end + State.line_cache = {} for i=1,#State.lines do State.line_cache[i] = {} -- cgit 1.4.1-2-gfad0 From c1f7f17f9caa70ffc4857f64db59ff41741b0caf Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 24 Nov 2023 19:19:29 -0800 Subject: bugfix: infinite loop inside a very narrow window I'm not sure this can trigger everywhere (I've only been able to exercise it in Lua Carousel), but it seems like a safety net worth having against future modifications by anybody. --- source_text.lua | 4 +++- text.lua | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/source_text.lua b/source_text.lua index f93e859..f60123e 100644 --- a/source_text.lua +++ b/source_text.lua @@ -128,7 +128,9 @@ function Text.populate_screen_line_starting_pos(State, line_index) -- long word; chop it at some letter -- We're not going to reimplement TeX here. local bpos = Text.nearest_pos_less_than(frag, State.width - x) - -- everything works if bpos == 0, but is a little inefficient + if x == 0 and bpos == 0 then + assert(false, ("Infinite loop while line-wrapping. Editor is %dpx wide; window is %dpx wide"):format(State.width, App.screen.width)) + end pos = pos + bpos local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos frag = string.sub(frag, boffset) diff --git a/text.lua b/text.lua index db51ac4..2383137 100644 --- a/text.lua +++ b/text.lua @@ -103,7 +103,9 @@ function Text.populate_screen_line_starting_pos(State, line_index) -- long word; chop it at some letter -- We're not going to reimplement TeX here. local bpos = Text.nearest_pos_less_than(frag, State.width - x) - -- everything works if bpos == 0, but is a little inefficient + if x == 0 and bpos == 0 then + assert(false, ("Infinite loop while line-wrapping. Editor is %dpx wide; window is %dpx wide"):format(State.width, App.screen.width)) + end pos = pos + bpos local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos frag = string.sub(frag, boffset) -- cgit 1.4.1-2-gfad0