From ff9470b8e2c154e496178815e31f3e682cc7e192 Mon Sep 17 00:00:00 2001 From: "Rodrigo B. de Oliveira" Date: Sun, 1 Dec 2024 17:47:52 -0300 Subject: [PATCH] Check tcp port range --- src/scheme_repl_server.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/scheme_repl_server.cpp b/src/scheme_repl_server.cpp index e4e5e64..78ba7a3 100644 --- a/src/scheme_repl_server.cpp +++ b/src/scheme_repl_server.cpp @@ -3,6 +3,7 @@ #include "scheme.hpp" #include #include +#include using namespace godot; using gd = UtilityFunctions; @@ -49,14 +50,21 @@ void SchemeReplServer::server_loop(int tcp_port, const String &tcp_bind_address) tcp_server->stop(); } -std::optional> parse_repl_args() { +using tcp_port_t = uint16_t; + +std::optional> parse_repl_args() { // TODO: accept --s7-tcp-address=
String tcp_bind_address = "127.0.0.1"; + String tcp_port_option = "--s7-tcp-port"; for (const auto &arg : OS::get_singleton()->get_cmdline_args()) { - if (arg.begins_with("--s7-tcp-port")) { + if (arg.begins_with(tcp_port_option)) { auto parts = arg.split("="); auto tcp_port = parts.size() > 1 ? parts[1].to_int() : 0; - return std::make_pair(tcp_port, tcp_bind_address); + auto max_tcp_port = std::numeric_limits::max(); + if (tcp_port < 0 || tcp_port >= max_tcp_port) { + gd::printerr(tcp_port_option, " argument must be a value between 0 and ", max_tcp_port, "."); + } + return std::make_pair(static_cast(tcp_port), std::move(tcp_bind_address)); } } return std::nullopt;