From da7958f89bdf6dbba2ce5fb35a05a492db979b94 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nicolas.pope@utu.fi>
Date: Wed, 8 Jun 2022 12:51:22 +0000
Subject: [PATCH] #5 Check port in URI parse

---
 src/uri.cpp | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/uri.cpp b/src/uri.cpp
index ff561f2..29d47ba 100644
--- a/src/uri.cpp
+++ b/src/uri.cpp
@@ -120,7 +120,13 @@ void URI::_parse(uri_t puri) {
         m_protostr = prototext;
 
         std::string porttext = std::string(uri.portText.first, uri.portText.afterLast - uri.portText.first);
-        m_port = atoi(porttext.c_str());
+        try {
+            m_port = std::stoi(porttext);
+            if (m_port < 0 || m_port >= 65535) {
+                throw FTL_Error("Port out of range");
+            }
+        } catch (const std::invalid_argument &e) {}
+
         m_userinfo = std::string(uri.userInfo.first, uri.userInfo.afterLast - uri.userInfo.first);
 
         for (auto h = uri.pathHead; h != NULL; h = h->next) {
-- 
GitLab