diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dcfe1578ec..e5550750d72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ ### Fixed * ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?) -* None. +* Crash when querying the size of a Object property through a link chain ([#6915](https://github.com/realm/realm-core/issues/6915), since v13.17.2) ### Breaking changes * None. diff --git a/src/realm/query_expression.cpp b/src/realm/query_expression.cpp index 1ce57573351..736a609ed3a 100644 --- a/src/realm/query_expression.cpp +++ b/src/realm/query_expression.cpp @@ -519,11 +519,16 @@ void LinkCount::evaluate(size_t index, ValueBase& destination) const Obj obj = m_link_map.get_target_table()->get_object(links[i]); auto val = obj._get(m_column_key.get_index()); size_t s; - if (val & 1) { + if (m_column_key.get_type() == col_type_Link && !m_column_key.is_collection()) { + // It is a single link column + s = (val == 0) ? 0 : 1; + } + else if (val & 1) { // It is a backlink column with just one value s = 1; } else { + // This is some kind of collection or backlink column s = _impl::get_collection_size_from_ref(to_ref(val), alloc); } destination.set(i, int64_t(s)); diff --git a/test/test_query.cpp b/test/test_query.cpp index 3a109ecc137..e4402ec6578 100644 --- a/test/test_query.cpp +++ b/test/test_query.cpp @@ -5640,6 +5640,7 @@ TEST(Query_NestedLinkCount) table->add_column_list(*table, "children"); table->add_column_dictionary(*table, "dictionary"); table->add_column_list(*table, "list"); + table->add_column(*table, "Object"); Obj o1 = table->create_object_with_primary_key(1); Obj o2 = table->create_object_with_primary_key(2); @@ -5650,6 +5651,10 @@ TEST(Query_NestedLinkCount) dict.insert("key", o3); o3.get_linklist("children").add(o2.get_key()); + o1.set("Object", o1.get_key()); + o2.set("Object", o2.get_key()); + o3.set("Object", o2.get_key()); + auto q = table->query("children.list.@size == 0"); CHECK_EQUAL(q.count(), 2); @@ -5676,7 +5681,13 @@ TEST(Query_NestedLinkCount) dict.insert("key4", o3); q = table->query("@links.TestClass.children.dictionary.@size == 5"); CHECK_EQUAL(q.count(), 1); // Only o2 -} + q = table->query("@links.TestClass.Object.@size > 0"); + CHECK_EQUAL(q.count(), 2); + q = table->query("@links.TestClass.Object.Object.@size > 0"); + CHECK_EQUAL(q.count(), 2); + q = table->query("Object.@links.TestClass.Object.@size > 0"); + CHECK_EQUAL(q.count(), 3); +} #endif // TEST_QUERY