Skip to content

warning: outside array bound #22218

@tomoaki0705

Description

@tomoaki0705
System information (version)
  • OpenCV => recent master ( 139c443 ), 3.4 ( 0a88f84 )
  • Operating System / Platform => Debian 11 Aarch64 / Ubuntu 22.04 Arm v7
  • Compiler => GCC 10.2 (Aarch64) / GCC 11.2 (Armv7)
Detailed description
[ 60%] Building CXX object modules/core/CMakeFiles/opencv_test_core.dir/test/test_mat.cpp.o
/opencv-fork/modules/core/test/test_mat.cpp: In member function 'void cv::Mat::forEach_impl(const Functor&)::PixelOperationWrapper::operator()(const cv::Range&) const [with _Tp = cv::Point3_<int>; Functor = opencv_test::{anonymous}::InitializerFunctor<cv::Point3_<int> >]':
/opencv-fork/modules/core/test/test_mat.cpp:595:24: warning: array subscript 2 is outside array bounds of 'cv::Mat::forEach_impl<cv::Point3_<int>, opencv_test::{anonymous}::InitializerFunctor<cv::Point3_<int> > >(const opencv_test::{anonymous}::InitializerFunctor<cv::Point3_<int> >&)::PixelOperationWrapper::rowCall2(int, int) const::Index [1]' [-Warray-bounds]
  595 |         pixel.z = idx[2];
      |                   ~~~~~^
In file included from /opencv-fork/modules/core/include/opencv2/core.hpp:3294,
                 from /opencv-fork/modules/ts/include/opencv2/ts.hpp:10,
                 from /opencv-fork/modules/core/test/test_precomp.hpp:7,
                 from /opencv-fork/modules/core/test/test_mat.cpp:4:
/opencv-fork/modules/core/include/opencv2/core/utility.hpp:681:15: note: while referencing 'idx'
  681 |             } idx = {{row, 0}};
      |               ^~~
/opencv-fork/modules/core/test/test_mat.cpp: In member function 'void cv::Mat::forEach_impl(const Functor&)::PixelOperationWrapper::operator()(const cv::Range&) const [with _Tp = cv::Vec<int, 5>; Functor = opencv_test::{anonymous}::InitializerFunctor5D<cv::Vec<int, 5> >]':
/opencv-fork/modules/core/test/test_mat.cpp:605:25: warning: array subscript 2 is outside array bounds of 'cv::Mat::forEach_impl<cv::Vec<int, 5>, opencv_test::{anonymous}::InitializerFunctor5D<cv::Vec<int, 5> > >(const opencv_test::{anonymous}::InitializerFunctor5D<cv::Vec<int, 5> >&)::PixelOperationWrapper::rowCall2(int, int) const::Index [1]' [-Warray-bounds]
  605 |         pixel[2] = idx[2];
      |                    ~~~~~^
In file included from /opencv-fork/modules/core/include/opencv2/core.hpp:3294,
                 from /opencv-fork/modules/ts/include/opencv2/ts.hpp:10,
                 from /opencv-fork/modules/core/test/test_precomp.hpp:7,
                 from /opencv-fork/modules/core/test/test_mat.cpp:4:
/opencv-fork/modules/core/include/opencv2/core/utility.hpp:681:15: note: while referencing 'idx'
  681 |             } idx = {{row, 0}};
      |               ^~~
/opencv-fork/modules/core/test/test_mat.cpp:606:25: warning: array subscript 3 is outside array bounds of 'cv::Mat::forEach_impl<cv::Vec<int, 5>, opencv_test::{anonymous}::InitializerFunctor5D<cv::Vec<int, 5> > >(const opencv_test::{anonymous}::InitializerFunctor5D<cv::Vec<int, 5> >&)::PixelOperationWrapper::rowCall2(int, int) const::Index [1]' [-Warray-bounds]
  606 |         pixel[3] = idx[3];
      |                    ~~~~~^
In file included from /opencv-fork/modules/core/include/opencv2/core.hpp:3294,
                 from /opencv-fork/modules/ts/include/opencv2/ts.hpp:10,
                 from /opencv-fork/modules/core/test/test_precomp.hpp:7,
                 from /opencv-fork/modules/core/test/test_mat.cpp:4:
/opencv-fork/modules/core/include/opencv2/core/utility.hpp:681:15: note: while referencing 'idx'
  681 |             } idx = {{row, 0}};
      |               ^~~
/opencv-fork/modules/core/test/test_mat.cpp:607:25: warning: array subscript 4 is outside array bounds of 'cv::Mat::forEach_impl<cv::Vec<int, 5>, opencv_test::{anonymous}::InitializerFunctor5D<cv::Vec<int, 5> > >(const opencv_test::{anonymous}::InitializerFunctor5D<cv::Vec<int, 5> >&)::PixelOperationWrapper::rowCall2(int, int) const::Index [1]' [-Warray-bounds]
  607 |         pixel[4] = idx[4];
      |                    ~~~~~^
In file included from /opencv-fork/modules/core/include/opencv2/core.hpp:3294,
                 from /opencv-fork/modules/ts/include/opencv2/ts.hpp:10,
                 from /opencv-fork/modules/core/test/test_precomp.hpp:7,
                 from /opencv-fork/modules/core/test/test_mat.cpp:4:
/opencv-fork/modules/core/include/opencv2/core/utility.hpp:681:15: note: while referencing 'idx'
  681 |             } idx = {{row, 0}};
      |               ^~~

The warning is triggered by this 2 structures, especially the idx[2], idx[3] and idx[4]

template<typename Pixel>
struct InitializerFunctor{
/// Initializer for cv::Mat::forEach test
void operator()(Pixel & pixel, const int * idx) const {
pixel.x = idx[0];
pixel.y = idx[1];
pixel.z = idx[2];
}
};
template<typename Pixel>
struct InitializerFunctor5D{
/// Initializer for cv::Mat::forEach test (5 dimensional case)
void operator()(Pixel & pixel, const int * idx) const {
pixel[0] = idx[0];
pixel[1] = idx[1];
pixel[2] = idx[2];
pixel[3] = idx[3];
pixel[4] = idx[4];
}
};

and the actual cause is here

int body[2];
operator const int*() const {
return reinterpret_cast<const int*>(this);
}
int& operator[](const int i) {
return body[i];
}
} idx = {{row, 0}};

If an index i = 2 or greater arrived to this function (operator []), then it'll access outside of array body
So, I feel that the warning message from the compiler is valid.
On the other hand, this file existed from years ago, and no issue was reported.
That indicates that the warning message is a false alarm.

So, basically, I'm confused which way to go.

A: ignore this and keep it as is
B: increase the size of body
C: add pragma around test_mat.cpp (suppress the warning)

Solution B doesn't seem valid at all, though.
The size is constant, but the parameter can be arbitrary number.

Steps to reproduce

Build under the above situation

Issue submission checklist
  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues,
    forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files: videos, images, onnx, etc

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions