Also add defensive code against invalid indices Fixes https://gitlab.com/kicad/code/kicad/issues/10325