From f0b7f3193c43fe04fcc02ac7e0ed0cfca79c2157 Mon Sep 17 00:00:00 2001 From: Nav Date: Sun, 27 Oct 2024 00:28:41 +0100 Subject: [PATCH] Corrected string->int conversion bug in `TargetPinDescriptor`. The `numericPosition` member should really be removed. Will revisit later. --- src/Services/StringService.cpp | 6 ++++++ src/Services/StringService.hpp | 2 ++ src/Targets/TargetPinDescriptor.cpp | 6 +++++- src/Targets/TargetPinDescriptor.hpp | 2 +- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Services/StringService.cpp b/src/Services/StringService.cpp index 40439afe..05284aa8 100644 --- a/src/Services/StringService.cpp +++ b/src/Services/StringService.cpp @@ -42,6 +42,12 @@ namespace Services return str; } + bool StringService::isNumeric(const std::string& str) { + return !std::any_of(str.begin(), str.end(), [] (unsigned char character) { + return std::isdigit(character); + }); + } + std::string StringService::toHex(std::uint64_t value) { auto stream = std::stringstream{}; stream << std::hex << std::setfill('0') << std::setw(16) << static_cast(value); diff --git a/src/Services/StringService.hpp b/src/Services/StringService.hpp index 766c6d0c..3f015b9f 100644 --- a/src/Services/StringService.hpp +++ b/src/Services/StringService.hpp @@ -17,6 +17,8 @@ namespace Services static bool isAscii(const std::string& str); static std::string replaceUnprintable(std::string str); + static bool isNumeric(const std::string& str); + static std::string toHex(std::uint64_t value); static std::string toHex(std::uint32_t value); static std::string toHex(std::uint16_t value); diff --git a/src/Targets/TargetPinDescriptor.cpp b/src/Targets/TargetPinDescriptor.cpp index e6556d2c..5830ec12 100644 --- a/src/Targets/TargetPinDescriptor.cpp +++ b/src/Targets/TargetPinDescriptor.cpp @@ -9,7 +9,11 @@ namespace Targets const std::optional& padKey ) : position(position) - , numericPosition(Services::StringService::toUint16(this->position, 10)) + , numericPosition( + Services::StringService::isNumeric(this->position) + ? Services::StringService::toUint16(this->position, 10) + : 0 + ) , padKey(padKey) {} } diff --git a/src/Targets/TargetPinDescriptor.hpp b/src/Targets/TargetPinDescriptor.hpp index 16f419ff..37fbe854 100644 --- a/src/Targets/TargetPinDescriptor.hpp +++ b/src/Targets/TargetPinDescriptor.hpp @@ -10,7 +10,7 @@ namespace Targets { public: std::string position; - std::uint16_t numericPosition; + std::uint16_t numericPosition; // TODO: Consider removing this bodge. Review after v1.1.0 std::optional padKey; TargetPinDescriptor(