From 1e39a76ca6f6721f43db518b3bab8d9dab71902b Mon Sep 17 00:00:00 2001 From: inogenous <123803852+inogenous@users.noreply.github.com> Date: Sat, 11 Jan 2025 09:44:54 +0100 Subject: [PATCH] Prevent segfault when search gives no hits Prevents segfault when searches during character creation gives no hits. To reproduce the issue being fixed: 1. Create custom character 2. Tab to "Scenario" 3. Press `/` to search, enter something that gives no hits, such as `99` 4. Segfault Similar segfault also happens for "Profession" and "Background" tab. Stacktrace of segfault being fixed: ``` 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 warning: 44 ./nptl/pthread_kill.c: No such file or directory (gdb) bt #0 __pthread_kill_implementation (threadid=, signo=signo@entry=6, no_tid=no_tid@entry=0) at ./nptl/pthread_kill.c:44 #1 0x00007ffff778acef in __pthread_kill_internal (threadid=, signo=6) at ./nptl/pthread_kill.c:78 #2 0x00007ffff7736c42 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26 #3 0x00007ffff771f4f0 in __GI_abort () at ./stdlib/abort.c:79 #4 0x00007ffff7ad4f9e in std::__glibcxx_assert_fail(char const*, int, char const*, char const*) () from /lib/x86_64-linux-gnu/libstdc++.so.6 #5 0x0000555556536759 in std::vector, std::allocator > >::operator[] (this=, __n=) at /usr/include/c++/14/bits/stl_vector.h:1130 #6 std::vector, std::allocator > >::operator[] (this=, __n=) at /usr/include/c++/14/bits/stl_vector.h:1128 #7 0x000055555653609b in operator() (__closure=0x555558499bd0, ui=...) at src/newcharacter.cpp:2544 #8 0x00005555569d5a09 in ui_adaptor::redraw_invalidated () at src/ui_manager.cpp:448 #9 0x00005555569d5ac7 in ui_adaptor::redraw () at src/ui_manager.cpp:353 #10 0x00005555569d5aee in ui_manager::redraw () at src/ui_manager.cpp:516 #11 0x00005555567242cd in query_popup::query_once (this=this@entry=0x7fffffff6890) at src/popup.cpp:295 #12 0x0000555556724b9f in query_popup::query (this=this@entry=0x7fffffff6890) at src/popup.cpp:405 #13 0x000055555664b8ff in popup (text="Nothing found.", flags=flags@entry=PF_NONE) at src/output.cpp:993 #14 0x0000555555aadb73 in popup<>(char const*) (mes=mes@entry=0x555556b4cd0a "Nothing found.") at src/output.h:529 #15 0x0000555556529731 in filter_entries, >(avatar &, int &, std::vector, std::allocator > > &, std::vector, std::allocator > > &, std::string, struct {...}, string_id) (u=..., cur_id=@0x7fffffff6ba4: 0, old_entries=std::vector of length 0, capacity 185, new_entries=std::vector of length 185, capacity 185 = {...}, filterstring="999", sorter=..., chosen_entry=...) at src/newcharacter.cpp:2415 #16 0x0000555556531f9b in set_profession (tabs=..., u=..., pool=) at src/newcharacter.cpp:2598 #17 avatar::create (this=this@entry=0x5555583e4150, type=type@entry=character_type::CUSTOM, tempname="") at src/newcharacter.cpp:837 #18 0x0000555556242bc0 in main_menu::new_character_tab (this=this@entry=0x7fffffffd9f0) at src/main_menu.cpp:1028 #19 0x0000555556245326 in main_menu::opening_screen (this=this@entry=0x7fffffffd9f0) at src/main_menu.cpp:884 #20 0x00005555557feb01 in main (argc=, argv=) at src/main.cpp:857 (gdb) frame 7 #7 0x000055555653609b in operator() (__closure=0x55557a911460, ui=...) at src/newcharacter.cpp:2544 2544 if( u.prof != &sorted_profs[i].obj() ) { (gdb) print i $1 = 0 (gdb) print sorted_profs $2 = std::vector of length 0, capacity 185 (gdb) print iStartPos $3 = (int &) @0x7fffffff6bc0: 0 (gdb) print end_pos $4 = 59 (gdb) print profs_length $5 = (size_t &) @0x7fffffff6c08: 185 (gdb) print iContentHeight $6 = (size_t &) @0x7fffffff6be0: 59 ``` --- src/newcharacter.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/newcharacter.cpp b/src/newcharacter.cpp index 3b0f937542d06..23f76dd0bcea8 100644 --- a/src/newcharacter.cpp +++ b/src/newcharacter.cpp @@ -2537,7 +2537,7 @@ void set_profession( tab_manager &tabs, avatar &u, pool_type pool ) //Draw options calcStartPos( iStartPos, cur_id, iContentHeight, profs_length ); - const int end_pos = iStartPos + std::min( iContentHeight, profs_length ); + const int end_pos = iStartPos + std::min( { iContentHeight, profs_length, sorted_profs.size() } ); std::string cur_prof_notes; for( int i = iStartPos; i < end_pos; i++ ) { nc_color col; @@ -2892,7 +2892,7 @@ void set_hobbies( tab_manager &tabs, avatar &u, pool_type pool ) //Draw options calcStartPos( iStartPos, cur_id, iContentHeight, hobbies_length ); - const int end_pos = iStartPos + std::min( iContentHeight, hobbies_length ); + const int end_pos = iStartPos + std::min( { iContentHeight, hobbies_length, sorted_hobbies.size() } ); std::string cur_hob_notes; for( int i = iStartPos; i < end_pos; i++ ) { nc_color col; @@ -3606,7 +3606,7 @@ void set_scenario( tab_manager &tabs, avatar &u, pool_type pool ) //Draw options calcStartPos( iStartPos, cur_id, iContentHeight, scens_length ); - const int end_pos = iStartPos + std::min( iContentHeight, scens_length ); + const int end_pos = iStartPos + std::min( { iContentHeight, scens_length, sorted_scens.size() } ); std::string current_scenario_notes; for( int i = iStartPos; i < end_pos; i++ ) { nc_color col;