Skip to content

Commit

Permalink
Add missing page exists check to entity_index_try_get_any
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed May 9, 2023
1 parent 726c934 commit 350f1aa
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
5 changes: 4 additions & 1 deletion flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -12538,8 +12538,11 @@ ecs_record_t* flecs_entity_index_try_get_any(

ecs_entity_index_page_t *page = ecs_vec_get_t(&index->pages,
ecs_entity_index_page_t*, page_index)[0];
ecs_record_t *r = &page->records[id & FLECS_ENTITY_PAGE_MASK];
if (!page) {
return NULL;
}

ecs_record_t *r = &page->records[id & FLECS_ENTITY_PAGE_MASK];
if (!r->dense) {
return NULL;
}
Expand Down
7 changes: 4 additions & 3 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
#define FLECS_HI_COMPONENT_ID (16)
#define FLECS_HI_ID_RECORD_ID (16)
#define FLECS_SPARSE_PAGE_BITS (6)
#define FLECS_ENTITY_PAGE_BITS (6)
#define FLECS_USE_OS_ALLOC
#endif

Expand Down Expand Up @@ -226,17 +227,17 @@
// #define FLECS_USE_OS_ALLOC

/** \def FLECS_ID_DESC_MAX
* Maximum number of components to add/remove in a single operation */
* Maximum number of ids to add ecs_entity_desc_t / ecs_bulk_desc_t */
#ifndef FLECS_ID_DESC_MAX
#define FLECS_ID_DESC_MAX (32)
#endif

/** \def FLECS_TERM_DESC_MAX
* Maximum number of terms in desc (larger, as these are temp objects) */
* Maximum number of terms in ecs_filter_desc_t */
#define FLECS_TERM_DESC_MAX (16)

/** \def FLECS_EVENT_DESC_MAX
* Maximum number of events to set in static array of observer descriptor */
* Maximum number of events in ecs_observer_desc_t */
#define FLECS_EVENT_DESC_MAX (8)

/** \def FLECS_VARIABLE_COUNT_MAX
Expand Down
5 changes: 4 additions & 1 deletion src/datastructures/entity_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ ecs_record_t* flecs_entity_index_try_get_any(

ecs_entity_index_page_t *page = ecs_vec_get_t(&index->pages,
ecs_entity_index_page_t*, page_index)[0];
ecs_record_t *r = &page->records[id & FLECS_ENTITY_PAGE_MASK];
if (!page) {
return NULL;
}

ecs_record_t *r = &page->records[id & FLECS_ENTITY_PAGE_MASK];
if (!r->dense) {
return NULL;
}
Expand Down
3 changes: 2 additions & 1 deletion test/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@
"invalid_add_pair_w_any_obj",
"invalid_pair_w_0",
"invalid_pair_w_0_rel",
"invalid_pair_w_0_obj"
"invalid_pair_w_0_obj",
"add_random_id"
]
}, {
"id": "Switch",
Expand Down
15 changes: 15 additions & 0 deletions test/api/src/Add.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <api.h>
#include <stdlib.h>

void Add_zero() {
install_test_abort();
Expand Down Expand Up @@ -396,3 +397,17 @@ void Add_invalid_pair_w_0_obj() {

ecs_add_id(world, e, ecs_pair(Tag, 0));
}

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

for (int i = 0; i < 10; i ++) {
ecs_entity_t id = rand() % (100 * 1000) + 1000;
ecs_ensure(world, id);
test_assert(ecs_is_alive(world, id));
ecs_entity_t e = ecs_new_w_id(world, id);
test_assert(ecs_has_id(world, e, id));
}

ecs_fini(world);
}
7 changes: 6 additions & 1 deletion test/api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ void Add_invalid_add_pair_w_any_obj(void);
void Add_invalid_pair_w_0(void);
void Add_invalid_pair_w_0_rel(void);
void Add_invalid_pair_w_0_obj(void);
void Add_add_random_id(void);

// Testsuite 'Switch'
void Switch_get_case_no_switch(void);
Expand Down Expand Up @@ -3487,6 +3488,10 @@ bake_test_case Add_testcases[] = {
{
"invalid_pair_w_0_obj",
Add_invalid_pair_w_0_obj
},
{
"add_random_id",
Add_add_random_id
}
};

Expand Down Expand Up @@ -12410,7 +12415,7 @@ static bake_test_suite suites[] = {
"Add",
NULL,
NULL,
25,
26,
Add_testcases
},
{
Expand Down

0 comments on commit 350f1aa

Please sign in to comment.