Visual Studio 中的警告 C6385



我似乎从Visual Studio 2019(16.5预览版,但也在16.4及更早版本中(代码分析工具收到错误的警告消息。这是一个错误,还是我真的只是错过了什么?

生成的警告(确切地(是:

警告 C6385:从"prodlist"读取无效数据:可读大小为"(size_t(*32+8"字节,但可能会读取"64"字节。

这是生成警告的代码(尽可能少(

#include <cstdint>
#include <string>
#include <iostream>
struct Product {
std::string price_profile;
};
int getNumRows() {
return 5;
}
Product *getProductsFromDB( int &numelements ) {
numelements = 0;
const int num_rows = getNumRows();
if ( num_rows == 0 ) {
numelements = 0;
return nullptr;
}
Product *prodlist = new Product[num_rows];
for ( int i = 0; i < num_rows; ++i ) {
prodlist[i].price_profile = "test"; // Warning on this line
}
numelements = num_rows;
return prodlist;
}
int main() {
int num_rows;
Product *prodlist = getProductsFromDB( num_rows );
for ( int i = 0; i < num_rows; ++i ) {
std::cout << prodlist[i].price_profile;
}
getchar();
}

如果我将price_profile更改为int(及其相应的值(,或者如果我将num_rows更改为常量(如5(,则警告会消失。

>似乎在Visual Studio 2019中,默认情况下Microsoft在C和C++代码上强制执行SAL分析规则,即使仍然有很多误报,就像你在这里的情况一样。

您现在可以做的一件事是禁用给出误报的警告:

#pragma warning(push)
#pragma warning(disable:6385)
Product *getProductsFromDB( int &numelements ) {
...
}
#pragma warning(pop)

最新更新