Skip to content

Commit 9f15e34

Browse files
sbrednikhinminggo
authored andcommitted
Avoid of type overflow while list size calculation. (#17697)
If _items container is empty `(length - 1) * _itemsMargin` will be large positive value. Using `size_t` causes type overflow and large positive sizes. Using int will cause negative size values. Direct check looks like the best way.
1 parent 8df9f1e commit 9f15e34

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

cocos/ui/UIListView.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void ListView::updateInnerContainerSize()
117117
case Direction::VERTICAL:
118118
{
119119
size_t length = _items.size();
120-
float totalHeight = (length - 1) * _itemsMargin + (_topPadding + _bottomPadding);
120+
float totalHeight = (length == 0) ? 0.0f : (length - 1) * _itemsMargin + (_topPadding + _bottomPadding);
121121
for (auto& item : _items)
122122
{
123123
totalHeight += item->getContentSize().height;
@@ -130,7 +130,7 @@ void ListView::updateInnerContainerSize()
130130
case Direction::HORIZONTAL:
131131
{
132132
size_t length = _items.size();
133-
float totalWidth = (length - 1) * _itemsMargin + (_leftPadding + _rightPadding);
133+
float totalWidth = (length == 0) ? 0.0f : (length - 1) * _itemsMargin + (_leftPadding + _rightPadding);
134134
for (auto& item : _items)
135135
{
136136
totalWidth += item->getContentSize().width;

0 commit comments

Comments
 (0)