Repository: opencv
Issue: warning: outside array bound
Labels: category: build/install,affected: 3.4,confirmed

<!--
If you have a question rather than reporting a bug please go to https://forum.opencv.org where you get much faster responses.
If you need further assistance please read [How To Contribute](https://github.com/opencv/opencv/wiki/How_to_contribute).

This is a template helping you to create an issue which can be processed as quickly as possible. This is the bug reporting section for the OpenCV library.
-->

##### System information (version)
<!-- Example
- OpenCV => 4.2
- Operating System / Platform => Windows 64 Bit
- Compiler => Visual Studio 2017
-->

- OpenCV => recent master ( 139c443770 ), 3.4 ( 0a88f84847 )
- 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]`

https://github.com/opencv/opencv/blob/057c3da82ab132d2c7dad5351a2184a01f900a1a/modules/core/test/test_mat.cpp#L589-L609

and the actual cause is here

https://github.com/opencv/opencv/blob/057c3da82ab132d2c7dad5351a2184a01f900a1a/modules/core/include/opencv2/core/utility.hpp#L674-L681

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

<!-- to add code example fence it with triple backticks and optional file extension
    ```.cpp
    // C++ code example
    ```
 or attach as .txt or .zip file
-->

##### Issue submission checklist

 - [ ] I report the issue, it's not a question
   <!--
   OpenCV team works with forum.opencv.org, Stack Overflow and other communities
   to discuss problems. Tickets with questions without a real issue statement will be
   closed.
   -->
 - [x] I checked the problem with documentation, FAQ, open issues,
       forum.opencv.org, Stack Overflow, etc and have not found any solution
   <!--
   Places to check:
   * OpenCV documentation: https://docs.opencv.org
   * FAQ page: https://github.com/opencv/opencv/wiki/FAQ
   * OpenCV forum: https://forum.opencv.org
   * OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
   * Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
   -->
 - [x] I updated to the latest OpenCV version and the issue is still there
   <!--
   master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
   OpenCV team supports only the latest release for each branch.
   The ticket is closed if the problem is not reproduced with the modern version.
   -->
 - [x] There is reproducer code and related data files: videos, images, onnx, etc
   <!--
   The best reproducer -- test case for OpenCV that we can add to the library.
   Recommendations for media files and binary files:
   * Try to reproduce the issue with images and videos in opencv_extra repository
     to reduce attachment size
   * Use PNG for images, if you report some CV related bug, but not image reader
     issue
   * Attach the image as an archive to the ticket, if you report some reader issue.
     Image hosting services compress images and it breaks the repro code.
   * Provide ONNX file for some public model or ONNX file with random weights,
     if you report ONNX parsing or handling issue. Architecture details diagram
     from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
   -->