From e3b30b694748d1da605fc7ba5278cea5620e5870 Mon Sep 17 00:00:00 2001 From: inogenous <123803852+inogenous@users.noreply.github.com> Date: Fri, 16 Aug 2024 13:05:34 +0200 Subject: [PATCH] Prevent crash from -1 array idx when searching zones Prevents the crash listed below that previously happened when searching for zones to add, but the search yielded no results: ``` Thread 1 "cataclysm-tiles" received signal SIGABRT, Aborted. __pthread_kill_implementation (threadid=, signo=signo@entry=6, no_tid=no_tid@entry=0) at ./nptl/pthread_kill.c:44 (gdb) bt #0 __pthread_kill_implementation (threadid=, signo=signo@entry=6, no_tid=no_tid@entry=0) at ./nptl/pthread_kill.c:44 #1 0x00007ffff787840f in __pthread_kill_internal (signo=6, threadid=) at ./nptl/pthread_kill.c:78 #2 0x00007ffff78294f2 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26 #3 0x00007ffff78124ed in __GI_abort () at ./stdlib/abort.c:79 #4 0x00007ffff7ad501e in std::__glibcxx_assert_fail(char const*, int, char const*, char const*) () from /lib/x86_64-linux-gnu/libstdc++.so.6 #5 0x0000555555de2639 in std::vector >::operator[] (this=, __n=) at /usr/include/c++/14/bits/stl_vector.h:1130 #6 std::vector >::operator[] (this=, __n=) at /usr/include/c++/14/bits/stl_vector.h:1128 #7 0x000055555699deac in uilist_impl::draw_controls (this=0x555599a181d0) at src/ui.cpp:138 #8 0x0000555555b7c312 in cataimgui::window::draw (this=0x555599a181d0) at src/cata_imgui.cpp:631 #9 cataimgui::window::draw (this=0x555599a181d0) at src/cata_imgui.cpp:600 #10 0x00005555569a2b1c in ui_adaptor::redraw_invalidated () at src/ui_manager.cpp:440 #11 0x00005555569a2bd9 in ui_adaptor::redraw () at src/ui_manager.cpp:345 #12 0x00005555569a2c00 in ui_manager::redraw () at src/ui_manager.cpp:508 #13 0x000055555699bfa4 in uilist::inputfilter (this=this@entry=0x7fffffffb9c8) at src/ui.cpp:531 #14 0x000055555699ead4 in uilist::query (this=this@entry=0x7fffffffb9c8, loop=loop@entry=true, timeout=timeout@entry=-1, allow_unfiltered_hotkeys=allow_unfiltered_hotkeys@entry=false) at src/ui.cpp:865 #15 0x0000555555c610d4 in zone_manager::query_type (this=this@entry=0x555557274060 , personal=personal@entry=false) at src/clzones.cpp:621 #16 0x0000555555ef3894 in game::zones_manager (this=this@entry=0x555558291db0) at src/game.cpp:6944 #17 0x0000555555f5d279 in game::do_regular_action (this=this@entry=0x555558291db0, act=@0x7fffffffcfec: ACTION_ZONES, player_character=..., mouse_target=std::optional [no contained value]) at src/handle_action.cpp:2438 #18 0x0000555555f60769 in game::handle_action (this=0x555558291db0) at src/handle_action.cpp:3172 #19 0x0000555555dcf14d in do_turn () at src/do_turn.cpp:579 #20 0x00005555557a1217 in main (argc=, argv=) at src/main.cpp:873 ``` gdb shows that `parent.selected` was negative: ``` (gdb) frame 7 138 parent.entries[parent.selected].desc.c_str() (gdb) print parent.selected $1 = -1 (gdb) print parent.entries.size() $2 = 63 ``` --- src/ui.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ui.cpp b/src/ui.cpp index a0b77fa07f881..fb1c076070b41 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -134,9 +134,13 @@ void uilist_impl::draw_controls() if( parent.desc_enabled ) { ImGui::Separator(); - cataimgui::draw_colored_text( parent.footer_text.empty() ? - parent.entries[parent.selected].desc.c_str() - : parent.footer_text.c_str() ); + std::string description; + if( !parent.footer_text.empty() ) { + description = parent.footer_text; + } else if( parent.selected >= -1 ) { + description = parent.entries[parent.selected].desc; + } + cataimgui::draw_colored_text( description ); } }