Skip to content

Commit

Permalink
Harmonize check on class name length
Browse files Browse the repository at this point in the history
  • Loading branch information
jedelbo committed Dec 1, 2023
1 parent e593a5f commit 2652252
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Fixed
* <How do the end-user experience this issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
* Update existing std exceptions thrown by the Sync Client to use Realm exceptions. ([#6255](https://github.com/realm/realm-core/issues/6255), since v10.2.0)
* Having a class name of length 57 would make client reset crash ([#????](https://github.com/realm/realm-core/issues/????), since v10.0.0)

### Breaking changes
* Update existing std exceptions thrown by the Sync Client to use Realm exceptions. ([PR #7141](https://github.com/realm/realm-core/pull/7141/files))
Expand Down
1 change: 0 additions & 1 deletion src/realm/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,6 @@ Table* Group::do_add_table(StringData name, Table::Type table_type, bool do_repl
return table;
}


Table* Group::create_table_accessor(size_t table_ndx)
{
REALM_ASSERT(m_tables.size() == m_table_accessors.size());
Expand Down
8 changes: 5 additions & 3 deletions src/realm/group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class GroupFriend;
/// A group is a collection of named tables.
///
class Group : public ArrayParent {
static constexpr StringData g_class_name_prefix = "class_";

public:
/// Construct a free-standing group. This group instance will be
/// in the attached state, but neither associated with a file, nor
Expand Down Expand Up @@ -272,7 +274,8 @@ class Group : public ArrayParent {
///
//@{

static const size_t max_table_name_length = 63;
static constexpr size_t max_table_name_length = 63;
static constexpr size_t max_class_name_length = max_table_name_length - g_class_name_prefix.size();

bool has_table(StringData name) const noexcept;
TableKey find_table(StringData name) const noexcept;
Expand All @@ -293,6 +296,7 @@ class Group : public ArrayParent {
using TableNameBuffer = std::array<char, max_table_name_length>;
static StringData class_name_to_table_name(StringData class_name, TableNameBuffer& buffer)
{
REALM_ASSERT(class_name.size() <= max_class_name_length);
char* p = std::copy_n(g_class_name_prefix.data(), g_class_name_prefix.size(), buffer.data());
size_t len = std::min(class_name.size(), buffer.size() - g_class_name_prefix.size());
std::copy_n(class_name.data(), len, p);
Expand Down Expand Up @@ -535,8 +539,6 @@ class Group : public ArrayParent {
}

private:
static constexpr StringData g_class_name_prefix = "class_";

// nullptr, if we're sharing an allocator provided during initialization
std::unique_ptr<SlabAlloc> m_local_alloc;
// in-use allocator. If local, then equal to m_local_alloc.
Expand Down
2 changes: 1 addition & 1 deletion src/realm/sync/instruction_applier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ BinaryData InstructionApplier::get_binary(StringBufferRange range) const

TableRef InstructionApplier::table_for_class_name(StringData class_name) const
{
if (class_name.size() >= Group::max_table_name_length - 6)
if (class_name.size() > Group::max_class_name_length)
bad_transaction_log("class name too long");
Group::TableNameBuffer buffer;
return m_transaction.get_table(Group::class_name_to_table_name(class_name, buffer));
Expand Down
1 change: 0 additions & 1 deletion src/realm/sync/noinst/client_history_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,6 @@ void ClientHistory::fix_up_client_file_ident_in_stored_changesets(Transaction& g

Group::TableNameBuffer buffer;
auto get_table_for_class = [&](StringData class_name) -> ConstTableRef {
REALM_ASSERT(class_name.size() < Group::max_table_name_length - 6);
return group.get_table(Group::class_name_to_table_name(class_name, buffer));
};

Expand Down
1 change: 0 additions & 1 deletion src/realm/sync/noinst/server/server_history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2132,7 +2132,6 @@ void ServerHistory::fixup_state_and_changesets_for_assigned_file_ident(Transacti
};

auto get_table_for_class = [&](StringData class_name) -> ConstTableRef {
REALM_ASSERT(class_name.size() < Group::max_table_name_length - 6);
Group::TableNameBuffer buffer;
return group.get_table(Group::class_name_to_table_name(class_name, buffer));
};
Expand Down

0 comments on commit 2652252

Please sign in to comment.