From 09d8c844bc88b0f7304c8f76ce4a891e60d2eab1 Mon Sep 17 00:00:00 2001 From: Benjamin Herr Date: Tue, 26 Jan 2010 03:11:16 +0100 Subject: [PATCH] strip entered IP addresses/hostname of whitespace before resolving It is easy to accidentally paste whitespace along with the actual connection information into the Play Network Game window's entry. Fortunately it is also easy for us to handle this by removing that whitespace before passing the string on. --- src/gui/C4StartupNetDlg.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/gui/C4StartupNetDlg.cpp b/src/gui/C4StartupNetDlg.cpp index 08504d37a..8a7b7b786 100644 --- a/src/gui/C4StartupNetDlg.cpp +++ b/src/gui/C4StartupNetDlg.cpp @@ -956,7 +956,28 @@ bool C4StartupNetDlg::DoOK() const char *szDirectJoinAddress = pJoinAddressEdt->GetText(); if (szDirectJoinAddress && *szDirectJoinAddress) { - AddReferenceQuery(szDirectJoinAddress, C4StartupNetListEntry::NRQT_DirectJoin); + // First do some acrobatics to avoid trying to resolve addresses with leading + // or trailing whitespace, which is easily pasted in with an IP address. + // We can trivially skip whitespace at the beginning, but we need a copy to + // omit whitespace at the end. + while(std::isspace(*szDirectJoinAddress)) + // skip whitespace at the beginning + ++szDirectJoinAddress; + if(!*szDirectJoinAddress) + // entry empty, apart from whitespace + return true; + const char *szDirectJoinAddressEnd = szDirectJoinAddress + std::strlen(szDirectJoinAddress) - 1; + while(std::isspace(*szDirectJoinAddressEnd)) + // skip whitespace at the end + --szDirectJoinAddressEnd; + if(*++szDirectJoinAddressEnd) + { + // Make a temporary copy of the part that is not trailing whitespace, if any + std::string strDirectJoinAddressStripped(szDirectJoinAddress, szDirectJoinAddressEnd - szDirectJoinAddress); + AddReferenceQuery(strDirectJoinAddressStripped.c_str(), C4StartupNetListEntry::NRQT_DirectJoin); + } + else + AddReferenceQuery(szDirectJoinAddress, C4StartupNetListEntry::NRQT_DirectJoin); // Switch focus to list so another OK joins the specified address SetFocus(pGameSelList, true); return true;