From 464cc34be5d266bb4155f13ab20edc35d86fd110 Mon Sep 17 00:00:00 2001 From: Sander Mertens Date: Sat, 4 May 2024 19:55:23 -0700 Subject: [PATCH] Fix incorrectly initialized context in query engine --- flecs.c | 10 ++++++++++ src/addons/rules/engine.c | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/flecs.c b/flecs.c index 4053fca62..d40015276 100644 --- a/flecs.c +++ b/flecs.c @@ -66256,6 +66256,8 @@ void flecs_rule_set_match( } ecs_iter_t *it = ctx->it; + ecs_assert(column >= 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(column < table->type.count, ECS_INTERNAL_ERROR, NULL); flecs_rule_it_set_column(it, field_index, column); ecs_id_t matched = flecs_rule_it_set_id(it, table, field_index, column); flecs_rule_set_vars(op, matched, ctx); @@ -66398,6 +66400,9 @@ bool flecs_rule_with( op_ctx->column = flecs_ito(int16_t, tr->index); op_ctx->remaining = flecs_ito(int16_t, tr->count); } else { + ecs_assert((op_ctx->remaining + op_ctx->column - 1) < table->type.count, + ECS_INTERNAL_ERROR, NULL); + ecs_assert(op_ctx->remaining >= 0, ECS_INTERNAL_ERROR, NULL); if (--op_ctx->remaining <= 0) { return false; } @@ -66742,6 +66747,8 @@ bool flecs_rule_self_up_with( bool result; if (id_only) { result = flecs_rule_with_id(op, redo, ctx); + ecs_rule_and_ctx_t *op_ctx = flecs_op_ctx(ctx, and); + op_ctx->remaining = 1; } else { result = flecs_rule_with(op, redo, ctx); } @@ -68208,8 +68215,11 @@ bool flecs_rule_populate( ecs_record_t *r = flecs_entities_get(ctx->world, src); ecs_table_t *src_table = r->table; if (src_table->column_map) { + ecs_assert(index <= src_table->type.count, + ECS_INTERNAL_ERROR, NULL); int32_t column = src_table->column_map[index - 1]; if (column != -1) { + ecs_assert(column >= 0, ECS_INTERNAL_ERROR, NULL); it->ptrs[i] = ecs_vec_get( &src_table->data.columns[column].data, it->sizes[i], diff --git a/src/addons/rules/engine.c b/src/addons/rules/engine.c index a1ad88cc9..754ff16af 100644 --- a/src/addons/rules/engine.c +++ b/src/addons/rules/engine.c @@ -375,6 +375,8 @@ void flecs_rule_set_match( } ecs_iter_t *it = ctx->it; + ecs_assert(column >= 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(column < table->type.count, ECS_INTERNAL_ERROR, NULL); flecs_rule_it_set_column(it, field_index, column); ecs_id_t matched = flecs_rule_it_set_id(it, table, field_index, column); flecs_rule_set_vars(op, matched, ctx); @@ -517,6 +519,9 @@ bool flecs_rule_with( op_ctx->column = flecs_ito(int16_t, tr->index); op_ctx->remaining = flecs_ito(int16_t, tr->count); } else { + ecs_assert((op_ctx->remaining + op_ctx->column - 1) < table->type.count, + ECS_INTERNAL_ERROR, NULL); + ecs_assert(op_ctx->remaining >= 0, ECS_INTERNAL_ERROR, NULL); if (--op_ctx->remaining <= 0) { return false; } @@ -861,6 +866,8 @@ bool flecs_rule_self_up_with( bool result; if (id_only) { result = flecs_rule_with_id(op, redo, ctx); + ecs_rule_and_ctx_t *op_ctx = flecs_op_ctx(ctx, and); + op_ctx->remaining = 1; } else { result = flecs_rule_with(op, redo, ctx); } @@ -2327,8 +2334,11 @@ bool flecs_rule_populate( ecs_record_t *r = flecs_entities_get(ctx->world, src); ecs_table_t *src_table = r->table; if (src_table->column_map) { + ecs_assert(index <= src_table->type.count, + ECS_INTERNAL_ERROR, NULL); int32_t column = src_table->column_map[index - 1]; if (column != -1) { + ecs_assert(column >= 0, ECS_INTERNAL_ERROR, NULL); it->ptrs[i] = ecs_vec_get( &src_table->data.columns[column].data, it->sizes[i],