From 350f1aa65fe2d9e165b704c6feb10f130b0d98d6 Mon Sep 17 00:00:00 2001 From: Sander Mertens Date: Tue, 9 May 2023 11:01:55 -0700 Subject: [PATCH] Add missing page exists check to entity_index_try_get_any --- flecs.c | 5 ++++- flecs.h | 7 ++++--- src/datastructures/entity_index.c | 5 ++++- test/api/project.json | 3 ++- test/api/src/Add.c | 15 +++++++++++++++ test/api/src/main.c | 7 ++++++- 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/flecs.c b/flecs.c index 46db11b2f..5488de3ea 100644 --- a/flecs.c +++ b/flecs.c @@ -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; } diff --git a/flecs.h b/flecs.h index e240c4275..4148cec28 100644 --- a/flecs.h +++ b/flecs.h @@ -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 @@ -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 diff --git a/src/datastructures/entity_index.c b/src/datastructures/entity_index.c index 9da8e0ad7..aac9f1ee9 100644 --- a/src/datastructures/entity_index.c +++ b/src/datastructures/entity_index.c @@ -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; } diff --git a/test/api/project.json b/test/api/project.json index 55641148d..5b287fa5b 100644 --- a/test/api/project.json +++ b/test/api/project.json @@ -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", diff --git a/test/api/src/Add.c b/test/api/src/Add.c index 549a690d9..8db56e411 100644 --- a/test/api/src/Add.c +++ b/test/api/src/Add.c @@ -1,4 +1,5 @@ #include +#include void Add_zero() { install_test_abort(); @@ -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); +} diff --git a/test/api/src/main.c b/test/api/src/main.c index 8c5ebc38a..0e10c0f8b 100644 --- a/test/api/src/main.c +++ b/test/api/src/main.c @@ -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); @@ -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 } }; @@ -12410,7 +12415,7 @@ static bake_test_suite suites[] = { "Add", NULL, NULL, - 25, + 26, Add_testcases }, {