From b586192536f32241429679d1361bb1e9387f4f5f Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Mon, 16 Sep 2024 18:50:34 +0300 Subject: [PATCH] fix(hex): now throws exc if not enough allocated mem --- include/base/hex.hpp | 4 ++-- src/hex.cpp | 16 ++++++++++++---- test/test-hex.cpp | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/include/base/hex.hpp b/include/base/hex.hpp index f1518c1..c6466f5 100644 --- a/include/base/hex.hpp +++ b/include/base/hex.hpp @@ -10,9 +10,9 @@ namespace hex bool isValid(const char *str) noexcept; bool isValid(std::string_view str) noexcept; - void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size) noexcept; + void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size); std::string encode(std::span data) noexcept; void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size); - std::vector decode(std::string_view str); + std::vector decode(std::string_view str) noexcept; } \ No newline at end of file diff --git a/src/hex.cpp b/src/hex.cpp index c3cf3db..5277954 100644 --- a/src/hex.cpp +++ b/src/hex.cpp @@ -35,9 +35,13 @@ namespace hex { return baseN::isValid(str, hexmap); } - void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size) noexcept + void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size) { - for (size_t i = 0; i < data_size && i * 2 + 1 < str_size; i++) + if (str_size < data_size * 2) + { + throw std::logic_error("hex::encode: not enough allocated length"); + } + for (size_t i = 0; i < data_size; i++) { str[i * 2] = hexdigits[data[i] >> 4]; str[i * 2 + 1] = hexdigits[data[i] & 0x0F]; @@ -59,12 +63,16 @@ namespace hex { throw std::logic_error("hex::decode: isn't hex"); } - for (size_t i = 0; i < data_size && i * 2 < str_size; i++) + if (data_size < str_size / 2) + { + throw std::logic_error("hex::decode: not enough allocated length"); + } + 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]]; } } - std::vector decode(std::string_view str) + std::vector decode(std::string_view str) noexcept { std::vector data(str.size() / 2); hex::decode(str.data(), str.size(), data.data(), data.size()); diff --git a/test/test-hex.cpp b/test/test-hex.cpp index 31252ed..3a18aab 100644 --- a/test/test-hex.cpp +++ b/test/test-hex.cpp @@ -7,6 +7,15 @@ TEST(hex, encode) { std::vector data = {0x74, 0x65, 0x73, 0x74}; EXPECT_EQ("74657374", encode(data)); + try + { + std::string str = ""; + encode(data.data(), data.size(), str.data(), str.size()); + } + catch (const std::exception &e) + { + EXPECT_STREQ(e.what(), "hex::encode: not enough allocated length"); + } } TEST(hex, encode_1e6) { @@ -17,6 +26,15 @@ TEST(hex, decode) { std::vector data = {0x61, 0x6e, 0x6f}; EXPECT_EQ(decode("616e6f"), data); + try + { + std::string str = ""; + decode("616", 3, data.data(), data.size()); + } + catch (const std::exception &e) + { + EXPECT_STREQ(e.what(), "hex::decode: isn't hex"); + } } TEST(hex, decode_1e6) {