From 65bdb3a5442af922c5b4fa9d19e448450b4be4c0 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Tue, 6 Sep 2022 15:50:37 +0000 Subject: [PATCH] dnn: eliminate GCC12 warning in total() call --- .../dnn/include/opencv2/dnn/shape_utils.hpp | 39 ++++++++++++++++--- .../dnn/src/layers/normalize_bbox_layer.cpp | 4 +- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/modules/dnn/include/opencv2/dnn/shape_utils.hpp b/modules/dnn/include/opencv2/dnn/shape_utils.hpp index fafd1bf725..3241a1a024 100644 --- a/modules/dnn/include/opencv2/dnn/shape_utils.hpp +++ b/modules/dnn/include/opencv2/dnn/shape_utils.hpp @@ -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; diff --git a/modules/dnn/src/layers/normalize_bbox_layer.cpp b/modules/dnn/src/layers/normalize_bbox_layer.cpp index 37202bf863..11dc911087 100644 --- a/modules/dnn/src/layers/normalize_bbox_layer.cpp +++ b/modules/dnn/src/layers/normalize_bbox_layer.cpp @@ -208,8 +208,8 @@ public: const float* inpData = inp0.ptr(); float* outData = outputs[0].ptr(); - 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)