Home > database >  Are these C6294 and C6201 warnings in Visual C 2022 legitimate?
Are these C6294 and C6201 warnings in Visual C 2022 legitimate?

Time:12-10

Visual C 2022 00482-90000-00000-AA381 produces these warnings:

Warning C6294   Ill-defined for-loop:  initial condition does not satisfy test.  Loop body not executed.    VisionResearch  C:\src\vcpkg\installed\x64-windows\include\opencv2\core\matx.hpp    562
Warning C6201   Index '2' is out of valid index range '0' to '1' for possibly stack allocated buffer 'this->val'.   VisionResearch  C:\src\vcpkg\installed\x64-windows\include\opencv2\core\matx.hpp    562 
    

in this and other similar OpenCV 4.5.4 functions:

template<typename _Tp, int m, int n> inline
Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1)
{
    CV_StaticAssert(channels >= 2, "Matx should have at least 2 elements.");
    val[0] = v0; val[1] = v1;
    for(int i = 2; i < channels; i  ) val[i] = _Tp(0);
}

Obviously, channels and val size can be greater than 2, which makes this warning incorrect. Am I missing something?


I dug a bit deeper, and this is an excerpt from the documentation of this warning (https://docs.microsoft.com/en-us/cpp/code-quality/c6294):

This warning indicates that a for-loop cannot be executed because the terminating condition is true. This warning suggests that the programmer's intent is not correctly captured.

Note cannot be executed, which is obviously incorrect.


More digging. It seems that this old standing issue is very low on the list of priorities for Visual Studio team. This bug https://developercommunity.visualstudio.com/t/Code-analysis-false-positive-warning-C62/759216 from 2019 is still not fixed.


Definitions of data members in the code snippet:

template<typename _Tp, int m, int n> class Matx
{
public:
    enum {
           rows     = m,
           cols     = n,
           channels = rows*cols,
....
    _Tp val[m*n]; //< matrix elements
};

CodePudding user response:

It seems the warning is related to the condition in the for loop

CV_StaticAssert(channels >= 2, "Matx should have at least 2 elements.");
val[0] = v0; val[1] = v1;
for(int i = 2; i < channels; i  ) val[i] = _Tp(0);

because it will not be executed if channels has the acceptable value equal to 2.

CodePudding user response:

Since the for loop is using channels but the size of val is m*n the compiler may have trouble determining that those are the same number. So the second warning is about the possibility of undefined behavior. Once the compiler has decided that undefined behavior is possible, all expectations of rational behavior have gone out the window. That includes the generation of compiler messages.

  • Related