From 0c28595bbf1447c616b6f41dcf858362d989f4d6 Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Tue, 17 Sep 2024 12:31:15 +0300 Subject: [PATCH] fix(hex): str_size argument --- include/base/hex.hpp | 2 +- src/hex.cpp | 12 ++++++------ test/test-hex.cpp | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/base/hex.hpp b/include/base/hex.hpp index c6466f5..861e424 100644 --- a/include/base/hex.hpp +++ b/include/base/hex.hpp @@ -7,7 +7,7 @@ namespace hex { - bool isValid(const char *str) noexcept; + bool isValid(const char *str, size_t str_size) noexcept; bool isValid(std::string_view str) noexcept; void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size); diff --git a/src/hex.cpp b/src/hex.cpp index 5277954..8bae1e3 100644 --- a/src/hex.cpp +++ b/src/hex.cpp @@ -27,9 +27,9 @@ static const int8_t hexmap[] = { namespace hex { - bool isValid(const char *str) noexcept + bool isValid(const char *str, size_t str_size) noexcept { - return baseN::isValid(str, hexmap); + return baseN::isValid(str, str_size, hexmap); } bool isValid(std::string_view str) noexcept { @@ -55,10 +55,6 @@ namespace hex } void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size) { - if (!hex::isValid(str)) - { - throw std::logic_error("hex::decode: out of digits map"); - } if (str_size % 2 != 0) { throw std::logic_error("hex::decode: isn't hex"); @@ -67,6 +63,10 @@ namespace hex { throw std::logic_error("hex::decode: not enough allocated length"); } + if (!hex::isValid(str, str_size)) + { + throw std::logic_error("hex::decode: out of digits map"); + } for (size_t i = 0; i * 2 < str_size; i++) { data[i] = hexmap[(int8_t)str[i * 2]] << 4 | hexmap[(int8_t)str[i * 2 + 1]]; diff --git a/test/test-hex.cpp b/test/test-hex.cpp index 3a18aab..68542e1 100644 --- a/test/test-hex.cpp +++ b/test/test-hex.cpp @@ -17,9 +17,9 @@ TEST(hex, encode) EXPECT_STREQ(e.what(), "hex::encode: not enough allocated length"); } } -TEST(hex, encode_1e6) +TEST(hex, encode_1e7) { - std::vector data(1e6); + std::vector data(1e7); encode(data); } TEST(hex, decode) @@ -36,9 +36,9 @@ TEST(hex, decode) EXPECT_STREQ(e.what(), "hex::decode: isn't hex"); } } -TEST(hex, decode_1e6) +TEST(hex, decode_1e7) { - std::string str(1e6, '0'); + std::string str(1e7, '0'); decode(str); }