[英]Warning C6385 in Visual Studio
I seem to get an erroneous warning message from Visual Studio 2019 (16.5 Preview but also in 16.4 and earlier) Code Analysis tool.我似乎从 Visual Studio 2019(16.5 预览版,但也在 16.4 及更早版本中)代码分析工具收到错误警告消息。 Is this a bug, or am I really just missing something?这是一个错误,还是我真的只是错过了什么?
The warning generated (exactly) is:生成的警告(确切地说)是:
warning C6385: Reading invalid data from 'prodlist': the readable size is '(size_t)*32+8' bytes, but '64' bytes may be read.警告 C6385:从“prodlist”读取无效数据:可读大小为“(size_t)*32+8”字节,但可以读取“64”字节。
Here's the code which generates the warning (as minimal as possible)这是生成警告的代码(尽可能少)
#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();
}
If I change the price_profile
to an int
(and its corresponding value), or if I change num_rows
to a constant (like 5
) then the warning goes away.如果我将price_profile
更改为int
(及其相应的值),或者如果我将num_rows
更改为常量(如5
),则警告消失。
It seems in Visual Studio 2019 Microsoft is enforcing SAL analysis rules on C and C++ code by default, even though there are still plenty of false positives like your case here.在 Visual Studio 2019 中,微软似乎默认对 C 和 C++ 代码强制执行 SAL 分析规则,尽管这里仍然有很多像你这样的误报。
One thing you can do for now is disable the warning giving a false positive:您现在可以做的一件事是禁用给出误报的警告:
#pragma warning(push)
#pragma warning(disable:6385)
Product *getProductsFromDB( int &numelements ) {
...
}
#pragma warning(pop)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.