Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when iterating empty type #1467

Merged
merged 1 commit into from
Dec 13, 2024
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
17 changes: 13 additions & 4 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27821,12 +27821,20 @@ struct type {
return flecs::id(world_, type_->array[index]);
}

flecs::id_t* begin() const {
return type_->array;
const flecs::id_t* begin() const {
if (type_ && type_->count) {
return type_->array;
} else {
return &empty_;
}
}

flecs::id_t* end() const {
return &type_->array[type_->count];
const flecs::id_t* end() const {
if (type_ && type_->count) {
return &type_->array[type_->count];
} else {
return &empty_;
}
}

/** Implicit conversion to type_t */
Expand All @@ -27836,6 +27844,7 @@ struct type {
private:
world_t *world_;
const type_t *type_;
flecs::id_t empty_;
};

/** #} */
Expand Down
17 changes: 13 additions & 4 deletions include/flecs/addons/cpp/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,20 @@ struct type {
return flecs::id(world_, type_->array[index]);
}

flecs::id_t* begin() const {
return type_->array;
const flecs::id_t* begin() const {
if (type_ && type_->count) {
return type_->array;
} else {
return &empty_;
}
}

flecs::id_t* end() const {
return &type_->array[type_->count];
const flecs::id_t* end() const {
if (type_ && type_->count) {
return &type_->array[type_->count];
} else {
return &empty_;
}
}

/** Implicit conversion to type_t */
Expand All @@ -71,6 +79,7 @@ struct type {
private:
world_t *world_;
const type_t *type_;
flecs::id_t empty_;
};

/** #} */
Expand Down
4 changes: 3 additions & 1 deletion test/cpp/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,9 @@
"get_mut_pair_second_invalid_type",
"ensure_pair_second_invalid_type",
"set_pair_second_invalid_type",
"get_ref_pair_second_invalid_type"
"get_ref_pair_second_invalid_type",
"iter_type",
"iter_empty_type"
]
}, {
"id": "Pairs",
Expand Down
38 changes: 38 additions & 0 deletions test/cpp/src/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4863,3 +4863,41 @@ void Entity_get_ref_pair_second_invalid_type(void) {
test_expect_abort();
world.entity().get_ref_second<Position>(v);
}

void Entity_iter_type(void) {
flecs::world world;

flecs::entity e = world.entity().add<Position>().add<Velocity>();

int32_t count = 0;
bool pos_found = false;
bool velocity_found = false;

for (auto id : e.type()) {
count ++;
if (id == world.id<Position>()) {
pos_found = true;
}
if (id == world.id<Velocity>()) {
velocity_found = true;
}
}

test_int(count, 2);
test_assert(pos_found == true);
test_assert(velocity_found == true);
}

void Entity_iter_empty_type(void) {
flecs::world world;

flecs::entity e = world.entity();

int32_t count = 0;

for (auto id : e.type()) {
count ++;
}

test_int(count, 0);
}
12 changes: 11 additions & 1 deletion test/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ void Entity_get_mut_pair_second_invalid_type(void);
void Entity_ensure_pair_second_invalid_type(void);
void Entity_set_pair_second_invalid_type(void);
void Entity_get_ref_pair_second_invalid_type(void);
void Entity_iter_type(void);
void Entity_iter_empty_type(void);

// Testsuite 'Pairs'
void Pairs_add_component_pair(void);
Expand Down Expand Up @@ -2520,6 +2522,14 @@ bake_test_case Entity_testcases[] = {
{
"get_ref_pair_second_invalid_type",
Entity_get_ref_pair_second_invalid_type
},
{
"iter_type",
Entity_iter_type
},
{
"iter_empty_type",
Entity_iter_empty_type
}
};

Expand Down Expand Up @@ -6882,7 +6892,7 @@ static bake_test_suite suites[] = {
"Entity",
NULL,
NULL,
276,
278,
Entity_testcases
},
{
Expand Down
Loading