From ca226e5d45523b01f047c43d978ed7015226f6c4 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Fri, 21 Jan 2022 20:08:06 +0100 Subject: [PATCH] #5 make sure we do not crash on illegal port --- .vscode/tasks.json | 10 +++--- DStarGatewayThread.cpp | 9 +++-- StringUtils.cpp | 17 +++++++++ StringUtils.h | 2 ++ Tests/StringUtils/stringToPort.cpp | 57 ++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 Tests/StringUtils/stringToPort.cpp diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 168ca95..ac3f100 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -12,10 +12,7 @@ "ENABLE_DEBUG=1", "USE_GPSD=1" ], - "group": { - "kind": "build", - "isDefault": true - }, + "group": "build", "problemMatcher": [] }, { @@ -28,7 +25,10 @@ "ENABLE_DEBUG=1", "USE_GPSD=1" ], - "group": "build", + "group": { + "kind": "build", + "isDefault": true + }, "problemMatcher": [] } ] diff --git a/DStarGatewayThread.cpp b/DStarGatewayThread.cpp index afc6533..3c030e2 100644 --- a/DStarGatewayThread.cpp +++ b/DStarGatewayThread.cpp @@ -22,7 +22,6 @@ #include #include #include -#include #include "DStarGatewayThread.h" #include "DStarGatewayDefs.h" @@ -804,9 +803,9 @@ void CDStarGatewayThread::processIrcDDB() if(!res) return; - if(m_dextraEnabled && m_dextraPool != nullptr && m_dextraPool->getIncomingHandler() != nullptr) { + auto remotePortInt = CStringUtils::stringToPort(remotePort); + if(m_dextraEnabled && remotePortInt > 0U && m_dextraPool != nullptr && m_dextraPool->getIncomingHandler() != nullptr) { CLog::logInfo("%s wants to DExtra connect to us, punching UDP Holes through NAT, remote port %s", address.c_str(), remotePort.c_str()); - auto remotePortInt = boost::lexical_cast(remotePort); m_dextraPool->getIncomingHandler()->traverseNat(address, remotePortInt); } else { @@ -820,9 +819,9 @@ void CDStarGatewayThread::processIrcDDB() if(!res) return; - if(m_dplusEnabled && m_dplusPool != nullptr && m_dplusPool->getIncomingHandler() != nullptr) { + auto remotePortInt = CStringUtils::stringToPort(remotePort); + if(m_dplusEnabled && remotePortInt > 0U && m_dplusPool != nullptr && m_dplusPool->getIncomingHandler() != nullptr) { CLog::logInfo("%s wants to DPlus connect to us, punching UDP Holes through NAT, remote port %s", address.c_str(), remotePort.c_str()); - auto remotePortInt = boost::lexical_cast(remotePort); m_dplusPool->getIncomingHandler()->traverseNat(address, remotePortInt); } else { diff --git a/StringUtils.cpp b/StringUtils.cpp index 63de202..9a98c88 100644 --- a/StringUtils.cpp +++ b/StringUtils.cpp @@ -16,6 +16,9 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#include +#include + #include "StringUtils.h" size_t CStringUtils::find_nth(const std::string& haystack, char needle, size_t nth) @@ -32,4 +35,18 @@ size_t CStringUtils::find_nth(const std::string& haystack, char needle, size_t n } return std::string::npos; +} + +unsigned int CStringUtils::stringToPort(const std::string& s) +{ + unsigned int port = 0U; + std::string ls = boost::trim_copy(s); + + if(!ls.empty() && std::all_of(ls.begin(), ls.end(), [](char c){ return c > '0' && c < '9'; })) { + auto portTemp = boost::lexical_cast(ls); + if(portTemp > 0U && portTemp <= 65535U) + port = portTemp; + } + + return port; } \ No newline at end of file diff --git a/StringUtils.h b/StringUtils.h index ccb7294..5df05ab 100644 --- a/StringUtils.h +++ b/StringUtils.h @@ -52,4 +52,6 @@ public: } static size_t find_nth(const std::string& haystack, char needle, size_t nth); + + static unsigned int stringToPort(const std::string& s); }; diff --git a/Tests/StringUtils/stringToPort.cpp b/Tests/StringUtils/stringToPort.cpp new file mode 100644 index 0000000..b4923ea --- /dev/null +++ b/Tests/StringUtils/stringToPort.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include + +#include "../../StringUtils.h" + +namespace StringUtilsTests +{ + + class StringUtils_stringToPort : public ::testing::Test { + + }; + + TEST_F(StringUtils_stringToPort, EmptyStringReturn0) + { + unsigned int port = CStringUtils::stringToPort(""); + + EXPECT_EQ(port, 0U); + } + + TEST_F(StringUtils_stringToPort, SpaceStringReturn0) + { + unsigned int port = CStringUtils::stringToPort(" "); + + EXPECT_EQ(port, 0U); + } + + TEST_F(StringUtils_stringToPort, NumberStringReturnsCorrectValue) + { + unsigned int port = CStringUtils::stringToPort("12345"); + + EXPECT_EQ(port, 12345U); + } + + TEST_F(StringUtils_stringToPort, NumberStringWithSpacesReturnsCorrectValue) + { + unsigned int port = CStringUtils::stringToPort(" 12345 "); + + EXPECT_EQ(port, 12345U); + } +} \ No newline at end of file