From 001afe6e9a93ce6ced781d5139ce12881ce27535 Mon Sep 17 00:00:00 2001 From: casey langen Date: Fri, 23 Oct 2020 11:01:54 -0700 Subject: [PATCH] Don't crash on malformed input when configuring a remote library. --- .../app/layout/RemoteLibrarySettingsLayout.cpp | 15 ++++++++++++--- src/plugins/server/HttpServer.cpp | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/musikcube/app/layout/RemoteLibrarySettingsLayout.cpp b/src/musikcube/app/layout/RemoteLibrarySettingsLayout.cpp index 7863669fa..7bdd43180 100644 --- a/src/musikcube/app/layout/RemoteLibrarySettingsLayout.cpp +++ b/src/musikcube/app/layout/RemoteLibrarySettingsLayout.cpp @@ -54,7 +54,16 @@ using namespace cursespp; #define R(x) (x->GetX() + x->GetWidth()) -static const std::string kArrowSymbol = "> "; +constexpr const char* kArrowSymbol = "> "; + +static int getIntFromTextInput(TextInput& input, int defaultValue = -1) { + try { + return std::stoi(input.GetText()); + } + catch (...) { + return defaultValue; + } +} static inline int longestStringLength(const std::vector&& keys) { int max = 0; @@ -228,8 +237,8 @@ void RemoteLibrarySettingsLayout::SyncPreferencesAndLayout() { void RemoteLibrarySettingsLayout::SavePreferences() { auto host = this->hostInput->GetText(); - auto wssPort = std::stoi(this->wssPortInput->GetText()); - auto httpPort = std::stoi(this->httpPortInput->GetText()); + auto wssPort = getIntFromTextInput(*this->wssPortInput, 7905); + auto httpPort = getIntFromTextInput(*this->httpPortInput, 7906); auto password = this->pwInput->GetText(); auto const wssTls = this->wssTlsCheckbox->IsChecked(); auto const httpTls = this->httpTlsCheckbox->IsChecked(); diff --git a/src/plugins/server/HttpServer.cpp b/src/plugins/server/HttpServer.cpp index a2b3351a1..5c134a03a 100644 --- a/src/plugins/server/HttpServer.cpp +++ b/src/plugins/server/HttpServer.cpp @@ -565,7 +565,7 @@ int HttpServer::HandleAudioTrackRequest( #endif if (response) { - /* 'format' will be valid if we're transocding. otherwise, extract the extension + /* 'format' will be valid if we're transcoding. otherwise, extract the extension from the filename. the client can use this as a hint when naming downloaded files */ std::string extension = format.size() ? format : fileExtension(filename); MHD_add_response_header(response, "X-musikcube-File-Extension", extension.c_str());