Visual Studio 2015 Code Analysis C6386 warns of buffer overrun

后端 未结 4 900
[愿得一人]
[愿得一人] 2020-12-06 12:06

I\'ve read a lot about the Visual Studio Code Analysis warning C8386, but can\'t figure out this particular issue with my code. I\'ve reduced it to the following small progr

4条回答
  •  旧时难觅i
    2020-12-06 12:29

    Since nNumItems is global, it would appear that code analyzer thinks that nNumItems might be set to SIZE_MAX elsewhere before your code executes. You can see this with a sample like:

    size_t nNumItems = 0;
    
    void foo()
    {
        nNumItems = SIZE_MAX;
    }
    void bar()
    {
        const size_t nTotal = 3 + 2 * nNumItems;
        auto nWords = new int[nTotal];
    
        nWords[0] = 1;
        nWords[1] = 2;
    }
    
    int main()
    {
        foo();
        bar();
    
        return 0;
    }
    

    Perhaps the best fix is to side-step the entire problem by using std::vector.

提交回复
热议问题