This repository demonstrates a code generation bug in Visual Studio C++ 2019
when using an x86-Release build and the gsl::at
function.
Please see comments in the source file. The bug only occurs with a Release build.
August 18, 2019: The problem no longer occurs in Visual Studio 16.2.2. Thank you for fixing the bug.
Older updates follow
Code generation in Visual Studio Version 16.3.0 Preview 1.0 has changed, and the problem no longer occurs. The problem still does occur in Version 16.2.0.
This uses a bootstrap and vcpkg
setup that I made. It runs git submodule update
and then
runs vcpkg
's bootstrap script. Then, it installs the dependencies in packages.txt
From Powershell:
PS> .\bootstrap.ps1
...
PS> .\install-deps.ps1
- Open folder in Visual Studio.
- Configure CMake.
- Select
x86-Release
configuration. - Run.
The checked in version will either thrown an exception or crash with a memory access
violation inside the VectorOwner::Check
function.
- Use of
gsl:at
on line 69. If this use is replaced with a direct array access, the bug does not occur.
// this version uses gsl::at, and triggers the later corruption error
const auto& GetFoo(int i) const noexcept { return gsl::at(foos, i); }
// use this version to prevent corruption in all cases
//const auto& GetFoo(int i) const noexcept { return foos[i]; }
- Accessing a member function via a temporary
this
pointer. The crash on line 100 does not occur if the result of callinghandle.Get()
is placed in a local variable before callingRun
. A successful case is shown on lines 96-97.
// this call to VectorOwner::Run succeeds in all cases, because a
// temporary is not used
auto& vo = handle.Get();
vo.Run(f);
// this next call fails in its call to Check() if GetFoo uses gsl::at
handle.Get().Run(f);