From 9a0d2c2f99df42d6e183e4d34c385553d3604fd1 Mon Sep 17 00:00:00 2001 From: Johnathon Slightham Date: Sat, 28 Feb 2026 22:37:58 -0500 Subject: [PATCH] Fix mDNS int parse crash, print errno on TCP connection failure --- include/util/log.h | 4 ++-- include/util/string.h | 13 +++++++++++++ src/TCPClient.cpp | 4 +++- src/mDNSDiscoveryService.cpp | 12 +++++++++--- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/include/util/log.h b/include/util/log.h index 31a69ac..654b0ea 100644 --- a/include/util/log.h +++ b/include/util/log.h @@ -11,7 +11,7 @@ #ifdef _WIN32 -void print_errno() { +inline void print_errno() { char errbuf[ERRBUF_SIZE]; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(), 0, errbuf, sizeof(errbuf), NULL); @@ -23,7 +23,7 @@ void print_errno() { #include #include -void print_errno() { +inline void print_errno() { spdlog::error("{}", strerror(errno)); } diff --git a/include/util/string.h b/include/util/string.h index 45b8ef9..7f5b412 100644 --- a/include/util/string.h +++ b/include/util/string.h @@ -7,6 +7,8 @@ #include #include #include +#include +#include inline std::vector split(const std::string &str, const char delimiter) { std::vector result; @@ -20,4 +22,15 @@ inline std::vector split(const std::string &str, const char delimit return result; } +inline bool is_integer(const std::string& s) { + if (s.empty()) return false; + + size_t start = 0; + if (s[0] == '-' || s[0] == '+') + start = 1; + + return start < s.size() && + std::all_of(s.begin() + start, s.end(), ::isdigit); +} + #endif // STRING_H diff --git a/src/TCPClient.cpp b/src/TCPClient.cpp index 8884d6b..ae146c4 100644 --- a/src/TCPClient.cpp +++ b/src/TCPClient.cpp @@ -9,6 +9,7 @@ #include "TCPClient.h" #include "constants.h" #include "spdlog/spdlog.h" +#include "util/log.h" constexpr auto SLEEP_WHILE_INITIALIZING = std::chrono::milliseconds(250); constexpr int PORT = 3001; @@ -55,7 +56,8 @@ int TCPClient::init() { #endif if (connect(this->m_socket, reinterpret_cast(&serv_addr), sizeof(serv_addr)) < 0) { - spdlog::error("[TCP] Connection failed"); + spdlog::error("[TCP] Connection failed to connect"); + print_errno(); deinit(); return -1; } diff --git a/src/mDNSDiscoveryService.cpp b/src/mDNSDiscoveryService.cpp index ec3257d..2338d09 100644 --- a/src/mDNSDiscoveryService.cpp +++ b/src/mDNSDiscoveryService.cpp @@ -300,17 +300,23 @@ std::optional mDNSDiscoveryService::parse_response(uint8_t *buf } if (split_string[0] == MODULE_ID_STR) { - response.id = stoi(split_string[1]); + if (is_integer(std::string(split_string[1]))) { + response.id = stoi(split_string[1]); + } } if (split_string[0] == MODULE_TYPE_STR) { - response.module_type = static_cast(stoi(split_string[1])); + if (is_integer(std::string(split_string[1]))) { + response.module_type = static_cast(stoi(split_string[1])); + } } if (split_string[0] == CONNECTED_MODULES_STR) { for (const auto connected_modules = split(split_string[1], ','); const auto &module_id : connected_modules) { - response.connected_module_ids.emplace_back(stoi(module_id)); + if (is_integer(module_id)) { + response.connected_module_ids.emplace_back(stoi(module_id)); + } } } }