From fdb705713e13220463995633f12ad2e8705abab6 Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Mon, 9 Oct 2023 10:57:56 -0400 Subject: [PATCH] listwidget.cpp: Fix possible divide-by-zero --- lib/widget/listwidget.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/widget/listwidget.cpp b/lib/widget/listwidget.cpp index bc971bf6ac9..a3456750fc6 100644 --- a/lib/widget/listwidget.cpp +++ b/lib/widget/listwidget.cpp @@ -111,6 +111,7 @@ void TabSelectionWidget::addOnTabChangedHandler(const W_TABSELECTION_ON_TAB_CHAN void TabSelectionWidget::setNumberOfTabs(size_t tabs) { + tabs = std::max(tabs, 1); size_t previousSize = tabButtons.size(); for (size_t n = tabs; n < tabButtons.size(); ++n) { @@ -144,8 +145,12 @@ void TabSelectionWidget::doLayoutAll() { TabSelectionStyle tabStyle; int scrollSpace = 0; - tabsAtOnce = tabs(); - tabStyle.tabSize = WzSize(width() / static_cast(tabs()), height()); + tabsAtOnce = std::max(tabs(), 1); + if (tabs() == 0 || width() == 0 || height() == 0) + { + return; + } + tabStyle.tabSize = WzSize((tabs() > 0) ? (width() / static_cast(tabs())) : 0, height()); tabStyle.scrollTabSize = WzSize(0, 0); tabStyle.tabGap = 0; tabStyle.tabAlignment = TabAlignment::LeftAligned; @@ -167,8 +172,8 @@ void TabSelectionWidget::doLayoutAll() prevTabPageButton->show(currentTab >= tabsAtOnce); nextTabPageButton->setGeometry(width() - tabStyle.scrollTabSize.width(), 0, tabStyle.scrollTabSize.width(), tabStyle.scrollTabSize.height()); nextTabPageButton->setImages(tabStyle.nextScrollTabImage, tabStyle.nextScrollTabImageDown, tabStyle.nextScrollTabImageHighlight); - nextTabPageButton->show(currentTab / tabsAtOnce < (tabs() - 1) / tabsAtOnce); - size_t numPages = std::max((tabs() + (tabsAtOnce - 1)) / tabsAtOnce, 1); + nextTabPageButton->show((tabsAtOnce > 0) ? currentTab / tabsAtOnce < (tabs() - 1) / tabsAtOnce : false); + size_t numPages = std::max((tabsAtOnce > 0) ? (tabs() + (tabsAtOnce - 1)) / tabsAtOnce : 1, 1); size_t numTabsOnLastPage = (tabs() > 0) ? ((tabs()-1) % tabsAtOnce) + 1 : 0; for (size_t n = 0; n < tabButtons.size(); ++n) {