Skip to content

Commit

Permalink
#1423 Fix linked list corruption in switch list
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens authored Nov 5, 2024
1 parent d2935ce commit 29f6606
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
2 changes: 2 additions & 0 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -31739,13 +31739,15 @@ bool flecs_switch_set(

if (!hdr[0]) {
hdr[0] = (uint64_t)element;
node->next = 0;
} else {
ecs_switch_node_t *head = flecs_switch_get_node(sw, (uint32_t)hdr[0]);
ecs_assert(head->prev == 0, ECS_INTERNAL_ERROR, NULL);
head->prev = element;

node->next = (uint32_t)hdr[0];
hdr[0] = (uint64_t)element;
ecs_assert(node->next != element, ECS_INTERNAL_ERROR, NULL);
}

node->prev = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/datastructures/switch_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,15 @@ bool flecs_switch_set(

if (!hdr[0]) {
hdr[0] = (uint64_t)element;
node->next = 0;
} else {
ecs_switch_node_t *head = flecs_switch_get_node(sw, (uint32_t)hdr[0]);
ecs_assert(head->prev == 0, ECS_INTERNAL_ERROR, NULL);
head->prev = element;

node->next = (uint32_t)hdr[0];
hdr[0] = (uint64_t)element;
ecs_assert(node->next != element, ECS_INTERNAL_ERROR, NULL);
}

node->prev = 0;
Expand Down
3 changes: 2 additions & 1 deletion test/core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@
"defer_add_union_relationship",
"defer_add_existing_union_relationship",
"defer_add_union_relationship_2_ops",
"defer_add_existing_union_relationship_2_ops"
"defer_add_existing_union_relationship_2_ops",
"stress_test_1"
]
}, {
"id": "Hierarchies",
Expand Down
53 changes: 53 additions & 0 deletions test/core/src/Union.c
Original file line number Diff line number Diff line change
Expand Up @@ -1444,3 +1444,56 @@ void Union_defer_add_existing_union_relationship_2_ops(void) {

ecs_fini(world);
}

void Union_stress_test_1(void) {
ecs_world_t *world = ecs_mini();

ECS_ENTITY(world, Rel, Union);
ECS_TAG(world, TgtA);
ECS_TAG(world, TgtB);
ECS_TAG(world, TgtC);

ecs_entity_t e1 = ecs_new(world);
ecs_entity_t e2 = ecs_new(world);

ecs_add_pair(world, e1, Rel, TgtA);
ecs_add_pair(world, e2, Rel, TgtA);
ecs_add_pair(world, e2, Rel, TgtB);

ecs_add_pair(world, e2, Rel, TgtA);
ecs_add_pair(world, e2, Rel, TgtB);
ecs_add_pair(world, e1, Rel, TgtC);

ecs_add_pair(world, e1, Rel, TgtA);
ecs_add_pair(world, e2, Rel, TgtB);
ecs_add_pair(world, e1, Rel, TgtC);

ecs_add_pair(world, e1, Rel, TgtA);
ecs_add_pair(world, e2, Rel, TgtA);
ecs_add_pair(world, e1, Rel, TgtC);

test_assert(ecs_has_pair(world, e1, Rel, TgtC));
test_assert(ecs_has_pair(world, e2, Rel, TgtA));

ecs_query_t *q_tgtA = ecs_query(world, {
.terms = {{ ecs_pair(Rel, TgtA) }}
});

ecs_query_t *q_tgtB = ecs_query(world, {
.terms = {{ ecs_pair(Rel, TgtB) }}
});

ecs_query_t *q_tgtC = ecs_query(world, {
.terms = {{ ecs_pair(Rel, TgtC) }}
});

test_int(ecs_query_count(q_tgtA).entities, 1);
test_int(ecs_query_count(q_tgtB).entities, 0);
test_int(ecs_query_count(q_tgtC).entities, 1);

ecs_query_fini(q_tgtA);
ecs_query_fini(q_tgtB);
ecs_query_fini(q_tgtC);

ecs_fini(world);
}
7 changes: 6 additions & 1 deletion test/core/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ void Union_defer_add_union_relationship(void);
void Union_defer_add_existing_union_relationship(void);
void Union_defer_add_union_relationship_2_ops(void);
void Union_defer_add_existing_union_relationship_2_ops(void);
void Union_stress_test_1(void);

// Testsuite 'Hierarchies'
void Hierarchies_setup(void);
Expand Down Expand Up @@ -4456,6 +4457,10 @@ bake_test_case Union_testcases[] = {
{
"defer_add_existing_union_relationship_2_ops",
Union_defer_add_existing_union_relationship_2_ops
},
{
"stress_test_1",
Union_stress_test_1
}
};

Expand Down Expand Up @@ -11208,7 +11213,7 @@ static bake_test_suite suites[] = {
"Union",
NULL,
NULL,
51,
52,
Union_testcases
},
{
Expand Down

0 comments on commit 29f6606

Please sign in to comment.