Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Cbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ vector<Decode> Decode::getCompoundElements(uint32_t countMultiplier, TW::byte ex

Decode::MapElements Decode::getMapElements() const {
auto elems = getCompoundElements(2, MT_map);
if (elems.size() % 2 != 0) {
throw std::invalid_argument("CBOR map with odd number of elements");
}
MapElements map;
for (auto i = 0ul; i < elems.size(); i += 2) {
map.emplace_back(make_pair(elems[i], elems[i + 1]));
Expand Down
10 changes: 10 additions & 0 deletions tests/common/CborTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,5 +517,15 @@ TEST(Cbor, GetTagElementNotTag) {
}
FAIL() << "Expected exception";
}

TEST(Cbor, Tag) {
// Indefinite-length CBOR map with odd element count
Data malformed = {0xBF, 0x01, 0xFF};

Cbor::Decode cbor(malformed);
ASSERT_TRUE(cbor.isValid());
EXPECT_THROW(cbor.getMapElements(), std::invalid_argument);
}

// clang-format on
} // namespace TW::Cbor::tests
Loading