Visual Studio 中的警告 C6385 [英] Warning C6385 in Visual Studio

查看:178
本文介绍了Visual Studio 中的警告 C6385的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. Is this a bug, or am I really just missing something?

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

The warning generated (exactly) is:

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

warning C6385: Reading invalid data from 'prodlist': the readable size is '(size_t)*32+8' bytes, but '64' bytes may be read.

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

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

如果我将 price_profile 更改为 int(及其相应的值),或者如果我将 num_rows 更改为常量(例如 5) 然后警告消失.

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.

推荐答案

似乎在 Visual Studio 2019 中,Microsoft 默认对 C 和 C++ 代码强制执行 SAL 分析规则,尽管这里仍然有很多误报,例如您的案例.

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.

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

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)

这篇关于Visual Studio 中的警告 C6385的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆