From bdc577b46b966204b44de984308a0326624c860c Mon Sep 17 00:00:00 2001 From: Andrew Scull Date: Tue, 30 Mar 2021 12:53:04 +0000 Subject: Error on unknown simple value Null, true and false are handled but any other simple value, or floating pointer value, would otherwise reach the CHECK() that was expected to be unreachable. Instead, error when an unhandled value is seen. Test: cppbor_host_test_external Change-Id: Idb1099bbc51649e5eb8f5fb4fa60553e8cbe7e8f --- src/cppbor_parse.cpp | 3 +++ tests/cppbor_test.cpp | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/cppbor_parse.cpp b/src/cppbor_parse.cpp index fcf0dac..ab01ee7 100644 --- a/src/cppbor_parse.cpp +++ b/src/cppbor_parse.cpp @@ -275,6 +275,9 @@ std::tuple parseRecursively(const uint8_t* begin, return handleBool(addlData, begin, pos, parseClient); case NULL_V: return handleNull(begin, pos, parseClient); + default: + parseClient->error(begin, "Unsupported floating-point or simple value."); + return {begin, nullptr}; } } CHECK(false); // Impossible to get here. diff --git a/tests/cppbor_test.cpp b/tests/cppbor_test.cpp index ef98519..ebdcc02 100644 --- a/tests/cppbor_test.cpp +++ b/tests/cppbor_test.cpp @@ -1734,6 +1734,24 @@ TEST(FullParserTest, IndefiniteArray) { message); } +TEST(FullParserTest, UnassignedSimpleValue) { + vector unassignedSimpleValue = {0xE5}; + + auto [item, pos, message] = parse(unassignedSimpleValue); + EXPECT_THAT(item, IsNull()); + EXPECT_EQ(pos, unassignedSimpleValue.data()); + EXPECT_EQ("Unsupported floating-point or simple value.", message); +} + +TEST(FullParserTest, FloatingPointValue) { + vector floatingPointValue = {0xFA, 0x12, 0x75, 0x34, 0x37}; + + auto [item, pos, message] = parse(floatingPointValue); + EXPECT_THAT(item, IsNull()); + EXPECT_EQ(pos, floatingPointValue.data()); + EXPECT_EQ("Unsupported floating-point or simple value.", message); +} + TEST(MapGetValueByKeyTest, Map) { Array compoundItem(1, 2, 3, 4, 5, Map(4, 5, "a", "b")); auto clone = compoundItem.clone(); -- cgit v1.2.3