Crash when attempting to create a micro via without prior routing of a track

Description

similar to #19750 (closed) but for microvia instead of blind/buried

crashs on kicad 8 and 9

Steps to reproduce

  1. Create a new project and open pcbnew.
  2. Select "Route tracks (x)", but don't route anything yet.
  3. Right click somewhere in the layout window.
  4. From the appearing context menu, select "Select layer and place micro via...". Kicad should crash.

gdb bt created with: gdb kicad

Click to expand gdb bt kicad 8.0.x
terminate called after throwing an instance of 'std::out_of_range'
  what():  bitset::set: __position (which is 18446744073709551615) >= _Nb (which is 60)

Thread 1 "kicad" received signal SIGABRT, Aborted.
__pthread_kill_implementation (no_tid=0, signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:44
Warnung: 44	./nptl/pthread_kill.c: Datei oder Verzeichnis nicht gefunden
(gdb) bt
#0  __pthread_kill_implementation (no_tid=0, signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:44
#1  __pthread_kill_internal (signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:78
#2  __GI___pthread_kill (threadid=<optimized out>, signo=signo@entry=6) at ./nptl/pthread_kill.c:89
#3  0x00007ffff484526e in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4  0x00007ffff48288ff in __GI_abort () at ./stdlib/abort.c:79
#5  0x00007ffff4ca5ff5 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff4cbb0da in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x00007ffff4ca5a55 in std::terminate() () from /lib/x86_64-linux-gnu/libstdc++.so.6
#8  0x00007ffff4cbb391 in __cxa_throw () from /lib/x86_64-linux-gnu/libstdc++.so.6
#9  0x00007ffff4ca94a0 in std::__throw_out_of_range_fmt(char const*, ...) () from /lib/x86_64-linux-gnu/libstdc++.so.6
#10 0x00007fffc27ce43e in std::bitset<60ul>::_M_check(unsigned long, char const*) const [clone .part.0] [clone .lto_priv.0] [clone .lto_priv.0] (__position=<optimized out>, 
    __s=__s@entry=0x7fffc384082e <error: Cannot access memory at address 0x7fffc384082e>, this=<optimized out>) at /usr/include/c++/13/bitset:823
#11 0x00007fffc27ce484 in std::bitset<60ul>::_M_check (__s=0x7fffc384082e <error: Cannot access memory at address 0x7fffc384082e>, __position=<optimized out>, this=<optimized out>) at /usr/include/c++/13/bitset:1168
#12 std::bitset<60ul>::set (this=<optimized out>, __position=<optimized out>, __val=<optimized out>) at /usr/include/c++/13/bitset:1170
#13 0x00007fffc3108ab7 in ROUTER_TOOL::handleLayerSwitch (this=0x5555568b3fd0, aEvent=<Fehler beim Lesen der Variable: Cannot access memory at address 0x7fffc19ffba8>, aForceVia=<optimized out>)
    at /usr/src/kicad-8.0.8-testing-180+202501260019~08a8081eb9~ubuntu24.04.1/kicad/pcbnew/router/router_tool.cpp:1021
#14 0x00007fffc32cf9d8 in std::function<int (TOOL_EVENT const&)>::operator()(TOOL_EVENT const&) const (__args#0=..., this=<Fehler beim Lesen der Variable: Cannot access memory at address 0x7fffc19fffa8>)
    at /usr/include/c++/13/bits/std_function.h:591
#15 COROUTINE<int, TOOL_EVENT const&>::callerStub (aData=<optimized out>) at /usr/src/kicad-8.0.8-testing-180+202501260019~08a8081eb9~ubuntu24.04.1/kicad/include/tool/coroutine.h:509
Backtrace stopped: Cannot access memory at address 0x7fffc383ee30

gdb bt created with: gdb --ex=run --args env LD_LIBRARY_PATH=/usr/lib/kicad-nightly/lib/x86_64-linux-gnu/:/usr/lib/kicad-nightly/lib/ KICAD_PATH=/usr/share/kicad-nightly KICAD_CONFIG_HOME="$HOME/.config/kicadnightly" /usr/lib/kicad-nightly/bin/kicad from https://forum.kicad.info/t/how-to-create-a-proper-bug-report-after-a-crash/25886/6

Click to expand gdb bt kicad 9.0.x
 
Thread 1 "kicad" received signal SIGSEGV, Segmentation fault.
0x00007fffa5ca0591 in sul::dynamic_bitset<unsigned long, std::allocator<unsigned long> >::set (value=true, pos=18446744073709551615, this=0x7fffd17ffb00)
    at /usr/src/kicad-202501251904+42b9a9604b~182~ubuntu24.04.1/kicad/thirdparty/dynamic_bitset/dynamic_bitset.h:2588
Warnung: 2588	/usr/src/kicad-202501251904+42b9a9604b~182~ubuntu24.04.1/kicad/thirdparty/dynamic_bitset/dynamic_bitset.h: Datei oder Verzeichnis nicht gefunden
(gdb) 
(gdb) bt
#0  0x00007fffa5ca0591 in sul::dynamic_bitset<unsigned long, std::allocator<unsigned long> >::set (value=true, pos=18446744073709551615, this=0x7fffd17ffb00)
    at /usr/src/kicad-202501251904+42b9a9604b~182~ubuntu24.04.1/kicad/thirdparty/dynamic_bitset/dynamic_bitset.h:2588
#1  BASE_SET::set (this=<optimized out>, pos=18446744073709551615) at /usr/src/kicad-202501251904+42b9a9604b~182~ubuntu24.04.1/kicad/include/base_set.h:121
#2  0x00007fffa6740729 in ROUTER_TOOL::handleLayerSwitch (this=0x555557f30460, aEvent=..., aForceVia=<optimized out>) at /usr/src/kicad-202501251904+42b9a9604b~182~ubuntu24.04.1/kicad/pcbnew/router/router_tool.cpp:1035
#3  0x00007fffa6919228 in std::function<int (TOOL_EVENT const&)>::operator()(TOOL_EVENT const&) const (__args#0=..., this=0x555558449fe8) at /usr/include/c++/13/bits/std_function.h:591
#4  COROUTINE<int, TOOL_EVENT const&>::callerStub (aData=<optimized out>) at /usr/src/kicad-202501251904+42b9a9604b~182~ubuntu24.04.1/kicad/include/tool/coroutine.h:513
#5  0x00007fffa6e81181 in make_fcontext () from /usr/lib/kicad-nightly/bin/_pcbnew.kiface
#6  0x0000000000000000 in ?? ()

KiCad Version

Application: KiCad PCB Editor x86_64 on x86_64

Version: 8.0.8-unknown-202501260019~08a8081eb9~ubuntu24.04.1, release build

Libraries:
	wxWidgets 3.2.4
	FreeType 2.13.2
	HarfBuzz 8.3.0
	FontConfig 2.15.0
	libcurl/8.5.0 OpenSSL/3.0.13 zlib/1.3 brotli/1.1.0 zstd/1.5.5 libidn2/2.3.7 libpsl/0.21.2 (+libidn2/2.3.7) libssh/0.10.6/openssl/zlib nghttp2/1.59.0 librtmp/2.3 OpenLDAP/2.6.7

Platform: Linux Mint 22.1, 64 bit, Little endian, wxGTK, X11, cinnamon, x11
OpenGL: Intel, Mesa Intel(R) Graphics (RPL-U), 4.6 (Compatibility Profile) Mesa 24.0.9-0ubuntu0.3

Build Info:
	Date: Jan 26 2025 00:19:03
	wxWidgets: 3.2.4 (wchar_t,wx containers) GTK+ 3.24
	Boost: 1.83.0
	OCC: 7.6.3
	Curl: 8.5.0
	ngspice: 43
	Compiler: GCC 13.3.0 with C++ ABI 1018

Build settings:
Edited by Stefan