Fix mDNS int parse crash, print errno on TCP connection failure

This commit is contained in:
2026-02-28 22:37:58 -05:00
parent a2f69f2183
commit 9a0d2c2f99
4 changed files with 27 additions and 6 deletions

View File

@@ -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 <errno.h>
#include <string.h>
void print_errno() {
inline void print_errno() {
spdlog::error("{}", strerror(errno));
}

View File

@@ -7,6 +7,8 @@
#include <sstream>
#include <string>
#include <vector>
#include <cctype>
#include <algorithm>
inline std::vector<std::string> split(const std::string &str, const char delimiter) {
std::vector<std::string> result;
@@ -20,4 +22,15 @@ inline std::vector<std::string> 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

View File

@@ -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<sockaddr *>(&serv_addr), sizeof(serv_addr)) < 0) {
spdlog::error("[TCP] Connection failed");
spdlog::error("[TCP] Connection failed to connect");
print_errno();
deinit();
return -1;
}

View File

@@ -300,21 +300,27 @@ std::optional<mDNSRobotModule> mDNSDiscoveryService::parse_response(uint8_t *buf
}
if (split_string[0] == MODULE_ID_STR) {
if (is_integer(std::string(split_string[1]))) {
response.id = stoi(split_string[1]);
}
}
if (split_string[0] == MODULE_TYPE_STR) {
if (is_integer(std::string(split_string[1]))) {
response.module_type = static_cast<ModuleType>(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) {
if (is_integer(module_id)) {
response.connected_module_ids.emplace_back(stoi(module_id));
}
}
}
}
}
ptr += a->data_length;
}