From e3c4fe6ebfdbb5fb1b71cd9635ed5bb357b1c968 Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Fri, 23 Apr 2021 20:09:00 +0100 Subject: [PATCH] Fix segfault when selecting a different iface. Signed-off-by: Andrea Odetti --- source/frontends/sdl/imgui/sdlsettings.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/source/frontends/sdl/imgui/sdlsettings.cpp b/source/frontends/sdl/imgui/sdlsettings.cpp index 09c68b0b..4508db52 100644 --- a/source/frontends/sdl/imgui/sdlsettings.cpp +++ b/source/frontends/sdl/imgui/sdlsettings.cpp @@ -557,30 +557,37 @@ namespace sa2 standardLabelText("Status", tfe_enabled ? "enabled" : "disabled"); if (ImGui::BeginCombo("Interface", static_cast(get_tfe_interface()))) { + std::vector ifaces; if (tfe_enumadapter_open()) { char *pname; char *pdescription; while (tfe_enumadapter(&pname, &pdescription)) + { + ifaces.push_back(pname); + lib_free(pdescription); + } + tfe_enumadapter_close(); + + for (const auto & iface : ifaces) { // must call it each time // as update_tfe_interface() will invalidate it const char * current = static_cast(get_tfe_interface()); - const bool isSelected = strcmp(pname, current) == 0; - if (ImGui::Selectable(pname, isSelected)) + const bool isSelected = strcmp(iface, current) == 0; + if (ImGui::Selectable(iface, isSelected)) { - update_tfe_interface(pname, nullptr); - RegSaveString(TEXT(REG_CONFIG), TEXT(REGVALUE_UTHERNET_INTERFACE), 1, pname); + // the following line interacts with tfe_enumadapter, so we must run it outside the above loop + update_tfe_interface(iface, nullptr); + RegSaveString(TEXT(REG_CONFIG), TEXT(REGVALUE_UTHERNET_INTERFACE), 1, iface); } if (isSelected) { ImGui::SetItemDefaultFocus(); } - lib_free(pname); - lib_free(pdescription); + lib_free(iface); } - tfe_enumadapter_close(); } ImGui::EndCombo(); }