In MSVC if I do this with address sanitizer turned on I get a runtime exception:
#include <vector>
int main()
{
std::vector<int> my_vector;
my_vector.reserve(20);
my_vector.assign({ 1, 1, 1 });
int* p = my_vector.data();
std::cout << p[3];
/*ERROR: AddressSanitizer: container-overflow on address 0x11d4a44ebf1c at pc 0x7ff6a882ae53 bp 0x0020d85cf720 sp 0x0020d85cf728
READ of size 4 at 0x11d4a44ebf1c thread T0*/
}
HINT: if you don't care about these errors you may set ASAN_OPTIONS=detect_container_overflow=0.
If you suspect a false positive see also: https://github.com/google/sanitizers/wiki/AddressSanitizerContainerOverflow.
I took me a while to figure out that this command is supposed to go in Environment variables under the “Debugging” menu. What I don’t get is that ASAN isn’t just a debug thing. For example I can run it in Release mode with the sanitizer on, why is it that ASAN_OPTIONS=detect_container_overflow=0 must be passed as a debugging environment variable? At first I thought it was supposed to be passed to the compiler options or as a preprocessor definition or something.
Is there a way to set this in CMake? I’ve tried:
set(ENV{ASAN_OPTIONS} "detect_container_overflow=0")
My CMake:
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
message("Using GCC compiler")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
message("Using Clang compiler")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
message("Using MSVC compiler")
add_compile_options(
/MP # MULTIPROCESSOR COMPILE
/Oi # ENABLE COMPILER INTRINSICS
/utf-8
$<$<CONFIG:DEBUG>:/fsanitize=address>
$<$<CONFIG:DEBUG>:/fsanitize=fuzzer>
)
set(ENV{ASAN_OPTIONS} "detect_container_overflow=0") # SET THIS BECAUSE SHADERC THROWS A FALSE POSITIVE ERROR BECAUSE THE LIBRARY WASN'T COMPILED WITH SANITIZER ON
else ()
message("Unknown compiler: ${CMAKE_CXX_COMPILER_ID}")
endif ()
This has no effect on the ASAN flags, so it’s not going into the right place.