From: Eunki, Hong Date: Tue, 7 Jan 2025 01:19:32 +0000 (+0900) Subject: [Tizen] Fix coverity issue : Dali::Vector<>.Size could be zero X-Git-Tag: accepted/tizen/9.0/unified/20250114.155918^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=80a1c87cf14532be06a8bb6a15e764992d706077;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git [Tizen] Fix coverity issue : Dali::Vector<>.Size could be zero Let's avoid size_t type overflow issue during binary search. It could be broken if mMarks.Size() was 0u. Also, if marks property value was not a float, also could be break (unlimited loop) To avoid these kind of issue, let we change the logic of binary search that is safe enough for overflow case. Change-Id: I5b27daf0915c64e9192d4821b7d11e7e5a45f356 Signed-off-by: Eunki, Hong --- diff --git a/dali-toolkit/internal/controls/slider/slider-impl.cpp b/dali-toolkit/internal/controls/slider/slider-impl.cpp index 20b498c89e..5f3fa961a9 100644 --- a/dali-toolkit/internal/controls/slider/slider-impl.cpp +++ b/dali-toolkit/internal/controls/slider/slider-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Samsung Electronics Co., Ltd. + * Copyright (c) 2025 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -862,16 +862,15 @@ float Slider::SnapToMark(float value) for(MarkList::SizeType i = 0; i < mMarks.Count(); ++i) { const Property::Value& propertyValue = mMarks[i]; - if(propertyValue.Get(mark)) - { - mark = MapValuePercentage(mark); + DALI_ASSERT_ALWAYS(propertyValue.Get(mark) && "Marker Property cannot get float!"); - float dist = fabsf(mark - value); - if(dist < closestDist) - { - closestDist = dist; - closestMark = mark; - } + mark = MapValuePercentage(mark); + + float dist = fabsf(mark - value); + if(dist < closestDist) + { + closestDist = dist; + closestMark = mark; } } @@ -883,34 +882,34 @@ bool Slider::MarkReached(float value, int& outIndex) const float MARK_TOLERANCE = GetMarkTolerance(); // Binary search - int head = 0, - tail = mMarks.Size() - 1; - int current; - float mark; + MarkList::SizeType head = 0u; + MarkList::SizeType tail = mMarks.Size(); + MarkList::SizeType current; + float mark; - while(head <= tail) + // For each range [head tail) (right-opened) + while(head < tail) { - current = head + (tail - head) / 2; + current = head + ((tail - head) >> 1); const Property::Value& propertyValue = mMarks[current]; - if(propertyValue.Get(mark)) - { - mark = MapValuePercentage(mark); + DALI_ASSERT_ALWAYS(propertyValue.Get(mark) && "Marker Property cannot get float!"); - if(fabsf(mark - value) < MARK_TOLERANCE) - { - outIndex = current; - return true; - } + mark = MapValuePercentage(mark); - if(value < mark) - { - tail = current - 1; - } - else - { - head = current + 1; - } + if(fabsf(mark - value) < MARK_TOLERANCE) + { + outIndex = static_cast(static_cast(current)); ///< Safety convertion from size type to integer. + return true; + } + + if(value < mark) + { + tail = current; + } + else + { + head = current + 1; } } @@ -1444,7 +1443,9 @@ double Slider::SliderAccessible::GetMaximum() const bool Slider::SliderAccessible::SetCurrent(double current) { if(current < GetMinimum() || current > GetMaximum()) + { return false; + } auto self = Toolkit::Slider::DownCast(Self()); auto& impl = Toolkit::GetImpl(self);