Dolphin, the GameCube and Wii emulator - Forums

Full Version: [PATCH] NetPlayDiag::FindGame(): do not return reference to temporary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

devm33

NetPlayDiag::FindGame() returns const reference to std:Confusedtring.
If game path isn't found, it returns "".
Because of implicit conversion, it results:
return std:Confusedtring("");
Using a reference to a destroyed object causes undefined behavior.
This commit fixes it by returning copied std:Confusedtring objects.

---
diff --git a/Source/Core/DolphinWX/Src/NetWindow.cpp b/Source/Core/DolphinWX/Src/NetWindow.cpp
index 1c8f054..e4bc377 100644
--- a/Source/Core/DolphinWX/Src/NetWindow.cpp
+++ b/Source/Core/DolphinWX/Src/NetWindow.cpp
@@ -409,7 +409,7 @@ void NetPlayDiag::GetNetSettings(NetSettings &settings)
settings.m_Controllers = SConfig::GetInstance().m_SIDevice[i];
}

-const std:Confusedtring& NetPlayDiag::FindGame()
+std:Confusedtring NetPlayDiag::FindGame()
{
// find path for selected game, sloppy..
for (u32 i = 0 ; auto game = m_game_list->GetISO(i); ++i)
diff --git a/Source/Core/DolphinWX/Src/NetWindow.h b/Source/Core/DolphinWX/Src/NetWindow.h
index 543c164..e798033 100644
--- a/Source/Core/DolphinWX/Src/NetWindow.h
+++ b/Source/Core/DolphinWX/Src/NetWindow.h
@@ -92,7 +92,7 @@ private:
void OnAdjustBuffer(wxCommandEvent& event);
void OnConfigPads(wxCommandEvent& event);
void GetNetSettings(NetSettings &settings);
- const std:Confusedtring& FindGame();
+ std:Confusedtring FindGame();

wxListBox* m_player_lbox;
wxTextCtrl* m_chat_text;
--

#
# I'm new and not familiar with dolphin-emu
# I concern about copying std:Confusedtring objects each time FindGame() is called
# may affect the performance of dolphin.
#
[/i]

jchadwick

Indeed, this is invalid C++ code. I just landed revision a791733c27fa with the fix. Performance is certainly not going to be a problem, so no need to worry.

By the way, these forums mangle whitespace something nasty. If you want patches to be applied directly from your diff, then I recommend you use git format-patch (you appear to be doing this correctly already) and upload the patch to somewhere else. (A pastebin will probably work, though I actually recommend something that hosts binary files- that way, we can be completely sure the file is byte-for-byte what it was when git produced it.)
Or use [ code ]

Code:
Foo
    Bar

devm33

First, thank you for applying the patch to master.

>> By the way, these forums mangle whitespace something nasty
Sorry, I missed it.

>> Or use [ code ]
Sure. I will use this next time. Thank you.