dnn: eliminate GCC12 warning in total() call
authorAlexander Alekhin <alexander.a.alekhin@gmail.com>
Tue, 6 Sep 2022 15:50:37 +0000 (15:50 +0000)
committerAlexander Alekhin <alexander.a.alekhin@gmail.com>
Wed, 14 Sep 2022 11:37:00 +0000 (11:37 +0000)
modules/dnn/include/opencv2/dnn/shape_utils.hpp
modules/dnn/src/layers/normalize_bbox_layer.cpp

index fafd1bf725dc973834ad3a53d3dfad7ea76c6a4c..3241a1a024316e0f22df97804b489535a27b4872 100644 (file)
@@ -160,22 +160,49 @@ static inline MatShape shape(int a0, int a1=-1, int a2=-1, int a3=-1)
 
 static inline int total(const MatShape& shape, int start = -1, int end = -1)
 {
-    if (start == -1) start = 0;
-    if (end == -1) end = (int)shape.size();
-
     if (shape.empty())
         return 0;
 
+    int dims = (int)shape.size();
+
+    if (start == -1) start = 0;
+    if (end == -1) end = dims;
+
+    CV_CheckLE(0, start, "");
+    CV_CheckLE(start, end, "");
+    CV_CheckLE(end, dims, "");
+
     int elems = 1;
-    CV_Assert(start <= (int)shape.size() && end <= (int)shape.size() &&
-              start <= end);
-    for(int i = start; i < end; i++)
+    for (int i = start; i < end; i++)
     {
         elems *= shape[i];
     }
     return elems;
 }
 
+// TODO: rename to countDimsElements()
+static inline int total(const Mat& mat, int start = -1, int end = -1)
+{
+    if (mat.empty())
+        return 0;
+
+    int dims = mat.dims;
+
+    if (start == -1) start = 0;
+    if (end == -1) end = dims;
+
+    CV_CheckLE(0, start, "");
+    CV_CheckLE(start, end, "");
+    CV_CheckLE(end, dims, "");
+
+    int elems = 1;
+    for (int i = start; i < end; i++)
+    {
+        elems *= mat.size[i];
+    }
+    return elems;
+}
+
 static inline MatShape concat(const MatShape& a, const MatShape& b)
 {
     MatShape c = a;
index 37202bf863e53adf9ad6bcb2cb0255394af239ff..11dc911087d8c9d80feeb26f6b6c8133c20ba854 100644 (file)
@@ -208,8 +208,8 @@ public:
         const float* inpData = inp0.ptr<float>();
         float* outData = outputs[0].ptr<float>();
 
-        size_t num = total(shape(inp0.size), 0, startAxis);
-        size_t numPlanes = total(shape(inp0.size), startAxis, endAxis + 1);
+        size_t num = total(inp0, 0, startAxis);
+        size_t numPlanes = total(inp0, startAxis, endAxis + 1);
         CV_Assert(num * numPlanes != 0);
         size_t planeSize = inp0.total() / (num * numPlanes);
         for (size_t n = 0; n < num; ++n)