I am on Windows, I don't want to use WSL or Visual Studio. I want the compiler to tell me about stupid bugs. But I cannot figure out a way to do it.
The problem: I cannot figure out a way to catch both of the bugs below:
#include<iostream>
#include<vector>
#include<set>
using namespace std;
int main() {
//case 1
vector<int> V(5);
cout << V[10] << endl;
//case 2
set<int> Set;
cout << *Set.begin();
}
Using clang
Clang allows me to use sanitizers on windows. I was not able to find a way to make them work in mingw. Here are my flags:
clang flagsclang++ -g -O0 -std=c++17 -Wall -Wno-sign-compare -Wno-unused-const-variable -Wno-logical-op-parentheses -Wno-keyword-macro -Wfloat-equal -Wshadow -Wno-conversion -Wno-sign-conversion -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize=undefined,address,float-divide-by-zero,float-cast-overflow
I can successfully catch the case 1, in the error window I can clearly see what the mistake and the line where it happens.
error message case1==21988==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x115ace5203c8 at pc 0x7ff6fc6a11c4 bp 0x002fb417f8e0 sp 0x002fb417f928
READ of size 4 at 0x115ace5203c8 thread T0
#0 0x7ff6fc6a11c3 in main C:\Users\rl\AppData\Local\Temp\CP Editor-KnCttI\sol.cpp:8
But I get no errors when I have the case2 bug.
Using mingw
I found 2 useful blogs on codeforces on this topic. There I found about the -D_GLIBCXX_DEBUG flag, but it does not work for me with clang, so I used mingw.
mingw flagsg++ -g -O0 -std=c++17 -Wall -Wno-sign-compare -Wno-unused-const-variable -Wno-logical-op-parentheses -Wno-keyword-macro -Wfloat-equal -Wshadow -Wno-conversion -Wno-sign-conversion -D_GLIBCXX_DEBUG
Case1 is worse than in clang case, because I don't see the specific line
error message case1C:/mingw64/lib/gcc/x86_64-w64-mingw32/13.2.0/include/c++/debug/vector:442:
In function:
std::debug::vector<_Tp, _Allocator>::reference std::debug::vector<_Tp,
_Allocator>::operator[](size_type) [with _Tp = int; _Allocator =
std::allocator<int>; reference = int&; size_type = long long unsigned
int]
Error: attempt to subscript container with out-of-bounds index 10, but
container only holds 5 elements.
Objects involved in the operation:
sequence "this" @ 000000904FBFFAD0 {
type = std::debug::vector<int, std::allocator<int> >;
}
But the case2 is better than with clang, at least I know that something is wrong and what type of mistake I made, but sadly I don't see the specific line.
error message case2C:/mingw64/lib/gcc/x86_64-w64-mingw32/13.2.0/include/c++/debug/safe_iterator.h:298:
In function:
gnu_debug::_Safe_iterator<_Iterator, _Sequence, _Category>::reference
gnu_debug::_Safe_iterator<_Iterator, _Sequence, _Category>::operator*()
const [with _Iterator = std::_Rb_tree_const_iterator<int>; _Sequence =
std::debug::set<int>; _Category = std::forward_iterator_tag; reference =
const int&]
Error: attempt to dereference a past-the-end iterator.
Object`s involved in the operation:
iterator "this" @ 0000008C1CBFFAA0 {
type = std::_Rb_tree_const_iterator<int> (constant iterator);
state = past-the-end;
references sequence with type 'std::debug::set<int, std::less<int>, std::allocator<int> >' @ 0000008C1CBFFA50
}
Both cases seem bad
I feel like I am doing something very wrong.
If you have a setup on windows that allows you to easily identify both of the bugs, I would be very interested to see how you did that.
I think this info can be useful to a lot of people who use windows and avoid using IDEs like visual studio (like people with low end pcs). I have seen too many people, including me, who manually search for this kind of bugs, and I think it should not be done like that.
compiler info clangC:\cp>clang -v
clang version 18.1.3
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
compiler info g++C:\cp>g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/13.2.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-13.2.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/buildroot/x86_64-1320-posix-seh-msvcrt-rt_v11-rev1/mingw64 --enable-host-shared --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --enable-libstdcxx-filesystem-ts=yes --disable-libssp --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/buildroot/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/buildroot/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/buildroot/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/buildroot/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev1, Built by MinGW-Builds project' --with-bugurl=https://github.com/niXman/mingw-builds CFLAGS='-O2 -pipe -fno-ident -I/c/buildroot/x86_64-1320-posix-seh-msvcrt-rt_v11-rev1/mingw64/opt/include -I/c/buildroot/prerequisites/x86_64-zlib-static/include -I/c/buildroot/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/buildroot/x86_64-1320-posix-seh-msvcrt-rt_v11-rev1/mingw64/opt/include -I/c/buildroot/prerequisites/x86_64-zlib-static/include -I/c/buildroot/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/buildroot/x86_64-1320-posix-seh-msvcrt-rt_v11-rev1/mingw64/opt/include -I/c/buildroot/prerequisites/x86_64-zlib-static/include -I/c/buildroot/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/buildroot/x86_64-1320-posix-seh-msvcrt-rt_v11-rev1/mingw64/opt/lib -L/c/buildroot/prerequisites/x86_64-zlib-static/lib -L/c/buildroot/prerequisites/x86_64-w64-mingw32-static/lib ' LD_FOR_TARGET=/c/buildroot/x86_64-1320-posix-seh-msvcrt-rt_v11-rev1/mingw64/bin/ld.exe --with-boot-ldflags=' -Wl,--disable-dynamicbase -static-libstdc++ -static-libgcc'
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 13.2.0 (x86_64-posix-seh-rev1, Built by MinGW-Builds project)
try using -ggdb3 instead of -g in the second approach
Sadly it does not change the output for me. Also tried -g3 and got the same result
ok
Hello, did you manage to figure it out? I've been wondering how to do the same thing.