Skip to content

Commit

Permalink
KOTOR: Protect against a division by zero in WidgetListBox
Browse files Browse the repository at this point in the history
If the listbox items have a height of zero (or no defined height at
all), calculating the number of widget would provoke a division by
zero.

This fixes Coverity Scan issue #1391035.
  • Loading branch information
DrMcCoy committed May 15, 2018
1 parent 73af885 commit 52c7785
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/engines/kotor/gui/widgets/listbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,18 @@ void WidgetListBox::setPermanentHighlightEnabled(bool value) {
_permanentHighlightEnabled = value;
}

const std::vector<KotORWidget *> &WidgetListBox::createItemWidgets(int count) {
const std::vector<KotORWidget *> &WidgetListBox::createItemWidgets(uint count) {
if (!_protoItem)
throw Common::Exception("ListBox widget has no PROTOITEM");

int widgetCount = count > 0 ? count :
getHeight() / _protoItem->getStruct("EXTENT").getSint("HEIGHT");
const int64 itemHeight = _protoItem->getStruct("EXTENT").getSint("HEIGHT");

for (int i = 0; i < widgetCount; ++i) {
Common::UString name = Common::UString::format("%s_ITEM_%d", _tag.c_str(), i);
uint widgetCount = count;
if ((count == 0) && (itemHeight > 0))
widgetCount = getHeight() / itemHeight;

for (uint i = 0; i < widgetCount; ++i) {
Common::UString name = Common::UString::format("%s_ITEM_%u", _tag.c_str(), i);
createItem(name);
}

Expand Down
2 changes: 1 addition & 1 deletion src/engines/kotor/gui/widgets/listbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class WidgetListBox : public KotORWidget {
* @param count number of items to add or 0 to add maximum number of
* items that fit into the listbox
*/
const std::vector<KotORWidget *> &createItemWidgets(int count = 0);
const std::vector<KotORWidget *> &createItemWidgets(uint count = 0);

void addItem(const Common::UString &text);
void refreshItemWidgets();
Expand Down

0 comments on commit 52c7785

Please sign in to comment.