简体   繁体   English

Visual C++ 2022 中的这些 C6294 和 C6201 警告是否合法?

[英]Are these C6294 and C6201 warnings in Visual C++ 2022 legitimate?

Visual C++ 2022 00482-90000-00000-AA381 produces these warnings: Visual C++ 2022 00482-90000-00000-AA381 产生以下警告:

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:在这个和其他类似的 OpenCV 4.5.4 功能中:

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.显然, channelsval的大小可以大于 2,这使得这个警告不正确。 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 ):我挖得更深了,这是此警告文档的摘录( 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. Note cannot be executed ,这显然是不正确的。


More digging.更多挖掘。 It seems that this old standing issue is very low on the list of priorities for Visual Studio team.似乎这个老问题在 Visual Studio 团队的优先级列表中非常低。 This bug https://developercommunity.visualstudio.com/t/Code-analysis-false-positive-warning-C62/759216 from 2019 is still not fixed.这个 2019 年的错误https://developercommunity.visualstudio.com/t/Code-analysis-false-positive-warning-C62/759216仍未修复。


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
};

It seems the warning is related to the condition in the for loop似乎警告与 for 循环中的条件有关

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 .因为如果channels的可接受值等于2 ,它将不会被执行。

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.由于 for 循环正在使用channels ,但val的大小为m*n ,编译器可能无法确定这些通道是否相同。 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.一旦编译器确定未定义的行为是可能的,所有对理性行为的期望都已超出 window。 That includes the generation of compiler messages.这包括生成编译器消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM