#5 make sure we do not crash on illegal port

pull/32/head
Geoffrey Merck 4 years ago
parent b22affb683
commit ca226e5d45

10
.vscode/tasks.json vendored

@ -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": []
}
]

@ -22,7 +22,6 @@
#include <chrono>
#include <iostream>
#include <fstream>
#include <boost/lexical_cast.hpp>
#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<unsigned int>(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<unsigned int>(remotePort);
m_dplusPool->getIncomingHandler()->traverseNat(address, remotePortInt);
}
else {

@ -16,6 +16,9 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#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<unsigned int>(ls);
if(portTemp > 0U && portTemp <= 65535U)
port = portTemp;
}
return port;
}

@ -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);
};

@ -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 <gtest/gtest.h>
#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);
}
}
Loading…
Cancel
Save

Powered by TurnKey Linux.