Fix coverity issue : Dali::Vector<>.Size could be zero 99/317699/2
authorEunki, Hong <eunkiki.hong@samsung.com>
Tue, 7 Jan 2025 01:19:32 +0000 (10:19 +0900)
committerEunki, Hong <eunkiki.hong@samsung.com>
Tue, 7 Jan 2025 02:00:50 +0000 (11:00 +0900)
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 <eunkiki.hong@samsung.com>
dali-toolkit/internal/controls/slider/slider-impl.cpp

index 20b498c89eebbdf5d880f680b1fcbb3d18c9e1f2..5f3fa961a9f976e9015f4d58b808a83921c3253b 100644 (file)
@@ -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<int>(static_cast<unsigned int>(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);