1

So I've asked this question before, but deleted it, because it was unclear, and I couldn't come up with a minimal reproducible example. I'm using a variable idx together with the ++-operator to iterate over an array to add some stand alone data to it, meaning I don't want to put it in a for-loop.

Now I did some "research" and I found that this

int main()
{
    int idx = 0;
    int integers[6];
    integers[idx++] = 0;
    integers[idx++] = 5;
    integers[idx++] = 3;
    integers[idx++] = 4;
    integers[idx++] = 1;
    integers[idx++] = 2;
}

does not cause a warning, while this

#include <string>

int main()
{
    int idx = 0;
    std::string* strings[6];
    strings[idx++] = new std::string("Some");
    strings[idx++] = new std::string("stand");
    strings[idx++] = new std::string("-");
    strings[idx++] = new std::string("alone");
    strings[idx++] = new std::string("data");
    strings[idx++] = new std::string(".");
}

causes the following warning when I run the program in debug mode (possibly because static code analysis is hard):

Warning C6386 Buffer overrun while writing to 'strings': the writable size is '48' bytes, but '56' bytes might be written.

The marked warning line isn't really important, since VS tends to mark a random line, but it's for me the fourth of the six memory allocations. So I was wondering why I'm getting this warning exactly, if and how I can get rid of it, without changing the idx++ to hardcoded indexes. I assume the problem is a combination of the ++-operator and allocating objects.

Important note: this is on Visual Studio, with Debug > Project Properties > C/C++ > General > Warning Level > Level 4 enabled. Also, this is all code causing the warning on my VS.

Here is all I can find in Visual Studio's about (maybe something else is causing this?)

Microsoft Visual Studio Enterprise 2019 (Version 16.5.3) VisualStudio.16.Release/16.5.3+30002.166

Microsoft .NET Framework (Version 4.8.03752)

Installed Version: Enterprise

Visual C++ 2019 00433-90002-07609-AA105 Microsoft Visual C++ 2019

ASP.NET and Web Tools 2019 16.5.236.49856 ASP.NET and Web Tools 2019

Azure App Service Tools v3.0.0 16.5.236.49856 Azure App Service Tools v3.0.0

C# Tools 3.5.0-beta4-20153-05+20b9af913f1b8ce0a62f72bea9e75e4aa3cf6b0e C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Color Theme Designer 1.0 Designer for creating new color themes

Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Cookiecutter 16.5.20041.1 Provides tools for finding, instantiating and customizing templates in cookiecutter format.

IntelliCode Extension 1.0 IntelliCode Visual Studio Extension Detailed Info

Microsoft Azure Tools 2.9 Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.30207.1

Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft MI-Based Debugger 1.0 Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards 1.0 Microsoft Visual C++ Wizards

Microsoft Visual Studio VC Package 1.0 Microsoft Visual Studio VC Package

NuGet Package Manager 5.5.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Productivity Power Tools 2017/2019 16.0 Installs the individual extensions of Productivity Power Tools 2017/2019

ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info

Python 16.5.20041.1 Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers.

Python - Conda support 16.5.20041.1 Conda support for Python projects.

Python - Django support 16.5.20041.1 Provides templates and integration for the Django web framework.

Python - IronPython support 16.5.20041.1 Provides templates and integration for IronPython-based projects.

Python - Profiling support 16.5.20041.1 Profiling support for Python projects.

Qt Visual Studio Tools 2.4.3 The Qt Visual Studio Tools allow developers to use the standard development environment without having to worry about any Qt-related build steps or tools.

Test Adapter for Boost.Test 1.0 Enables Visual Studio's testing tools with unit tests written for Boost.Test. The use terms and Third Party Notices are available in the extension installation directory.

Test Adapter for Google Test 1.0 Enables Visual Studio's testing tools with unit tests written for Google Test. The use terms and Third Party Notices are available in the extension installation directory.

TypeScript Tools 16.0.20225.2001 TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools 3.5.0-beta4-20153-05+20b9af913f1b8ce0a62f72bea9e75e4aa3cf6b0e Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Tools for CMake 1.0 Visual Studio Tools for CMake

Community
  • 1
  • 1
  • Maybe Visual Studio thinks that the `string` constructor might overwrite the index? I don't believe it has the ability. – John Dvorak Apr 26 '20 at 15:08
  • Can't duplicate - https://godbolt.org/z/fQhkGn – Richard Critten Apr 26 '20 at 15:16
  • Uh, I'm not familiar with that site, what exactly are you trying to show me, and what do you mean with "can't duplicate"? It throws a lot of windows at me when I open it, so I'm not sure where to look. –  Apr 26 '20 at 15:20
  • The 1st pane is your test program; the 4th pane is MSVC compiling with no messages at warning level 4 (the middle 2 are clang and gcc). So with MSVC 19.21 (you can change the version) I can't reproduce the problem you are having. – Richard Critten Apr 26 '20 at 15:21
  • Ah, I see. Have you also tried opening it in VS with warning level 4? –  Apr 26 '20 at 15:24
  • Exactly which version of MSVC compiler are you using ? – Richard Critten Apr 26 '20 at 15:24
  • I'm sorry, I can't find it at the moment. Do you know where to look for the compiler? –  Apr 26 '20 at 15:29
  • You ccould check the version of the compiler in `Help-> About Microsoft Visual Studio`. I set the `Warning Level` to `Level 4` in visual studio 16.5.3 and 15.9.17 and couldn't reproduce your issue. – Jeaninez - MSFT Apr 27 '20 at 03:19
  • My visual studio version is 16.5.3; I'm not sure if every version of VS has it's own debugger, so I put all about in the question. –  Apr 27 '20 at 09:07
  • Are you getting this warning when using Analyze->Run Code Analysis->On Solution. In my opinion it may be a code analysis false positive. I suggest you could post the issue to the [Developer Community](https://developercommunity.visualstudio.com/spaces/8/index.html) for better help. I suggest you could refer to the link:https://stackoverflow.com/a/41944194/11872808 – Jeaninez - MSFT Apr 28 '20 at 06:26
  • I've added the link, but I didn't run the Code Analysis test, if that was a question :/ –  Apr 28 '20 at 07:52

0 Answers0