From 001d48f6cc78232f9eecbb858ff9daa1de4edb4c Mon Sep 17 00:00:00 2001 From: poire-z Date: Wed, 19 May 2021 22:57:46 +0200 Subject: [PATCH] Fix a few possible crashes - TextWidget: avoid crash with small max_width (could happen when opening menu on a small emulator window) - TextWidget: avoid crash if re-used after :free() - ReaderHighlight:clear(): fix possible crash when scheduled and run after document closed - DictQuickLookup: minor canSearch() tweak ('selected_text' is normally available only on multiple words selection, but is currently available on single word selection thanks to some unrelated side effect) --- frontend/apps/reader/modules/readerhighlight.lua | 4 ++++ frontend/ui/widget/dictquicklookup.lua | 2 +- frontend/ui/widget/textwidget.lua | 6 ++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/frontend/apps/reader/modules/readerhighlight.lua b/frontend/apps/reader/modules/readerhighlight.lua index 9bebadd82..fb0bdae12 100644 --- a/frontend/apps/reader/modules/readerhighlight.lua +++ b/frontend/apps/reader/modules/readerhighlight.lua @@ -379,6 +379,10 @@ function ReaderHighlight:clear(clear_id) end end self.clear_id = nil -- invalidate id + if not self.ui.document then + -- might happen if scheduled and run after document is closed + return + end if self.ui.document.info.has_pages then self.view.highlight.temp = {} else diff --git a/frontend/ui/widget/dictquicklookup.lua b/frontend/ui/widget/dictquicklookup.lua index bc314bd44..3d45cf472 100644 --- a/frontend/ui/widget/dictquicklookup.lua +++ b/frontend/ui/widget/dictquicklookup.lua @@ -75,7 +75,7 @@ function DictQuickLookup:canSearch() end else -- This is to prevent an ineffective button when we're launched from the Reader's menu. - if self.ui.highlight.selected_text then + if self.highlight then return true end end diff --git a/frontend/ui/widget/textwidget.lua b/frontend/ui/widget/textwidget.lua index 13409b92f..2837352cc 100644 --- a/frontend/ui/widget/textwidget.lua +++ b/frontend/ui/widget/textwidget.lua @@ -214,6 +214,9 @@ function TextWidget:_measureWithXText() -- no bold: xtext does synthetized bold with normal metrics end local max_width = self.max_width - reserved_width + if max_width <= 0 then -- avoid _xtext:makeLine() crash + max_width = self.max_width + end if self.truncate_left then line_start = self._xtext:getSegmentFromEnd(max_width) end @@ -311,7 +314,6 @@ end function TextWidget:setText(text) if text ~= self.text then self.text = text - self._updated = false self:free() end end @@ -324,7 +326,6 @@ dbg:guard(TextWidget, "setText", function TextWidget:setMaxWidth(max_width) if max_width ~= self.max_width then self.max_width = max_width - self._updated = false self:free() end end @@ -378,6 +379,7 @@ function TextWidget:free() self._xtext:free() self._xtext = nil end + self._updated = false end function TextWidget:onCloseWidget()