Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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/odr/internal/common/element_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ template <typename T, typename Id = ElementIdentifier>
class SortedSideTable final {
public:
T &emplace(const ElementIdentifier id, T value) {
if (id > std::numeric_limits<Id>::max()) {
throw std::out_of_range("SortedSideTable::emplace: identifier too wide");
}
if (!m_entries.empty() && m_entries.back().first >= id) {
throw std::invalid_argument(
"SortedSideTable::emplace: identifier out of order");
Expand Down
10 changes: 8 additions & 2 deletions src/odr/internal/common/text_cursor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <odr/internal/util/string_util.hpp>

#include <cstddef>
#include <stdexcept>
#include <string_view>

namespace odr::internal {
Expand Down Expand Up @@ -32,7 +33,12 @@ class TextCursor {
return c;
}

void advance(const std::size_t count) { m_rest.remove_prefix(count); }
void advance(const std::size_t count) {
if (count > m_rest.size()) {
throw std::out_of_range("TextCursor::advance: past end of input");
}
m_rest.remove_prefix(count);
}

/// Back to a view @ref rest answered earlier, undoing what was read since.
void seek(const std::string_view at) { m_rest = at; }
Expand All @@ -46,7 +52,7 @@ class TextCursor {
/// Whitespace ahead of it is filler; the text itself is the token.
[[nodiscard]] bool consume(const char c) {
skip_whitespace();
if (peek() != c) {
if (empty() || peek() != c) {
return false;
}
advance(1);
Expand Down
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ add_executable(odr_test

"src/internal/cfb/cfb_archive_test.cpp"

"src/internal/common/element_registry_test.cpp"
"src/internal/common/filesystem_test.cpp"
"src/internal/common/list_numbering_test.cpp"
"src/internal/common/path_test.cpp"
"src/internal/common/table_cursor_test.cpp"
"src/internal/common/sheet_cell_source_test.cpp"
"src/internal/common/table_range_test.cpp"
"src/internal/common/text_cursor_test.cpp"

"src/internal/crypto/crypto_util_test.cpp"

Expand Down
23 changes: 23 additions & 0 deletions test/src/internal/common/element_registry_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <odr/internal/common/element_registry.hpp>

#include <gtest/gtest.h>

#include <cstdint>
#include <limits>
#include <stdexcept>

using namespace odr::internal;

TEST(SortedSideTable, rejects_wide_ids_without_corrupting_order) {
SortedSideTable<std::uint32_t, std::uint8_t> table;
table.emplace(1, 10);
table.emplace(255, 20);
EXPECT_THROW(table.emplace(256, 30), std::out_of_range);
EXPECT_THROW(
table.emplace(std::numeric_limits<odr::ElementIdentifier>::max(), 40),
std::out_of_range);
EXPECT_EQ(table.at(1), 10);
EXPECT_EQ(table.at(255), 20);
EXPECT_EQ(table.find(256), nullptr);
EXPECT_EQ(table.find(0), nullptr);
}
20 changes: 20 additions & 0 deletions test/src/internal/common/text_cursor_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <odr/internal/common/text_cursor.hpp>

#include <gtest/gtest.h>

#include <stdexcept>
#include <string_view>

using namespace odr::internal;

TEST(TextCursor, rejects_overread_and_distinguishes_nul_from_end) {
TextCursor cursor(std::string_view("\0x", 2));
EXPECT_THROW(cursor.advance(3), std::out_of_range);
EXPECT_EQ(cursor.rest().size(), 2);
EXPECT_TRUE(cursor.consume('\0'));
EXPECT_EQ(cursor.take(), 'x');
EXPECT_FALSE(cursor.consume('\0'));
EXPECT_TRUE(cursor.empty());
EXPECT_NO_THROW(cursor.advance(0));
EXPECT_THROW(cursor.advance(1), std::out_of_range);
}
Loading