From 8adc867a2e0d0a7706681d343de29d6e04fd93f1 Mon Sep 17 00:00:00 2001 From: hashlag Date: Thu, 13 Aug 2026 01:17:54 +0300 Subject: [PATCH] PadderIso7816: Do not allow to pad empty ranges. --- Chaos/Padding/PadderIso7816.hpp | 5 ++++ ChaosTests/Padding/PadderIso7816Tests.cpp | 33 +++++++++++------------ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Chaos/Padding/PadderIso7816.hpp b/Chaos/Padding/PadderIso7816.hpp index 4acd000..547f6e7 100644 --- a/Chaos/Padding/PadderIso7816.hpp +++ b/Chaos/Padding/PadderIso7816.hpp @@ -7,6 +7,7 @@ #include "Padding/Padder.hpp" #include "Service/Branchless.hpp" +#include "Service/ChaosException.hpp" namespace Chaos::Padding { @@ -28,6 +29,10 @@ public: *it = 0; } } + else + { + throw Service::ChaosException("PadderIso7816::Pad(): invalid range"); + } } struct ComputeUnpadResult diff --git a/ChaosTests/Padding/PadderIso7816Tests.cpp b/ChaosTests/Padding/PadderIso7816Tests.cpp index 233ac6c..c9c0a17 100644 --- a/ChaosTests/Padding/PadderIso7816Tests.cpp +++ b/ChaosTests/Padding/PadderIso7816Tests.cpp @@ -1,5 +1,6 @@ #include #include +#include "TestHelpers/AssertThrowEx.hpp" #include #include @@ -8,6 +9,7 @@ #include "Padding/PadderIso7816.hpp" #include "Padding/Padder.hpp" +#include "Service/ChaosException.hpp" using namespace Chaos::Padding; @@ -45,7 +47,7 @@ TEST(PadIso7816Tests, PadTest) ASSERT_EQ(expected, fact); } - for (int i = 0; i < 256; ++i) + for (int i = 1; i < 256; ++i) { std::vector fact(i, 0xff); @@ -58,6 +60,19 @@ TEST(PadIso7816Tests, PadTest) } } +TEST(PadIso7816Tests, PadInvalidRangeTest) +{ + { + std::array out = {}; + + ASSERT_THROW_EX(PadderIso7816::Pad(out.begin(), out.begin()), + Chaos::Service::ChaosException, + { + ASSERT_EQ("PadderIso7816::Pad(): invalid range", ex.GetMessage()); + }); + } +} + TEST(PadIso7816Tests, PadOutIteratorUsageTest) { { @@ -92,22 +107,6 @@ TEST(PadIso7816Tests, PadOutIteratorUsageTest) PadderIso7816::Pad(fact.begin() + 3, fact.end() - 3); ASSERT_EQ(expected, fact); } - - { - std::array fact = - { - 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, - 0xbb, 0xbb, 0xbb, 0xbb, 0xbb - }; - std::array expected = - { - 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, - 0xbb, 0xbb, 0xbb, 0xbb, 0xbb - }; - - PadderIso7816::Pad(fact.begin() + 5, fact.begin() + 5); - ASSERT_EQ(expected, fact); - } } template