Skip to content

Commit

Permalink
Fix crash when serializing query plan for trivial query
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Aug 20, 2024
1 parent 9a8c826 commit 5414ea5
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 6 deletions.
8 changes: 6 additions & 2 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -42075,8 +42075,12 @@ void flecs_json_serialize_query_plan(

bool prev_color = ecs_log_enable_colors(true);
char *plan = ecs_query_plan(q);
flecs_json_string_escape(buf, plan);
ecs_os_free(plan);
if (plan) {
flecs_json_string_escape(buf, plan);
ecs_os_free(plan);
} else {
flecs_json_null(buf);
}
ecs_log_enable_colors(prev_color);
}

Expand Down
8 changes: 6 additions & 2 deletions src/addons/json/serialize_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,12 @@ void flecs_json_serialize_query_plan(

bool prev_color = ecs_log_enable_colors(true);
char *plan = ecs_query_plan(q);
flecs_json_string_escape(buf, plan);
ecs_os_free(plan);
if (plan) {
flecs_json_string_escape(buf, plan);
ecs_os_free(plan);
} else {
flecs_json_null(buf);
}
ecs_log_enable_colors(prev_color);
}

Expand Down
4 changes: 3 additions & 1 deletion test/meta/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,9 @@
"anonymous_component",
"anonymous_tag_recycled",
"anonymous_pair_recycled",
"anonymous_component_recycled"
"anonymous_component_recycled",
"serialize_plan_trivial_query",
"serialize_plan_nontrivial_query"
]
}, {
"id": "MetaUtils",
Expand Down
58 changes: 58 additions & 0 deletions test/meta/src/SerializeQueryInfoToJson.c
Original file line number Diff line number Diff line change
Expand Up @@ -691,3 +691,61 @@ void SerializeQueryInfoToJson_anonymous_component_recycled(void) {

ecs_fini(world);
}

void SerializeQueryInfoToJson_serialize_plan_trivial_query(void) {
ecs_world_t *world = ecs_init();

ECS_COMPONENT(world, Position);

ecs_query_t *q = ecs_query(world, {
.terms = {{ ecs_id(Position) }}
});

test_assert(q != NULL);

ecs_iter_t it = ecs_query_iter(world, q);
ecs_iter_to_json_desc_t desc = {
.serialize_query_plan = true,
.query = q
};

char *json = ecs_iter_to_json(&it, &desc);
char *expect = flecs_asprintf("{\"query_plan\":null, \"results\":[]}");
test_json(json, expect);
ecs_os_free(json);
ecs_os_free(expect);

ecs_query_fini(q);

ecs_fini(world);
}

void SerializeQueryInfoToJson_serialize_plan_nontrivial_query(void) {
ecs_world_t *world = ecs_init();

ECS_COMPONENT(world, Position);

ecs_add_pair(world, ecs_id(Position), EcsOnInstantiate, EcsInherit);

ecs_query_t *q = ecs_query(world, {
.terms = {{ ecs_id(Position) }}
});

test_assert(q != NULL);

ecs_iter_t it = ecs_query_iter(world, q);
ecs_iter_to_json_desc_t desc = {
.serialize_query_plan = true,
.query = q
};

char *json = ecs_iter_to_json(&it, &desc);
char *expect = flecs_asprintf("{\"query_plan\":\"[[0;49m 0. [[[0;37m-1[[0;49m, [[0;32m 1[[0;49m] setids \\n[[0;49m 1. [[[0;37m 0[[0;49m, [[0;32m 2[[0;49m] selfupid [[0;32m$[[0;49m[[[0;32mthis[[0;49m] ([[0;34mPosition[[0;49m)\\n[[0;49m 2. [[[0;37m 1[[0;49m, [[0;32m 3[[0;49m] yield \\n\", \"results\":[]}");
test_json(json, expect);
ecs_os_free(json);
ecs_os_free(expect);

ecs_query_fini(q);

ecs_fini(world);
}
12 changes: 11 additions & 1 deletion test/meta/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,8 @@ void SerializeQueryInfoToJson_anonymous_component(void);
void SerializeQueryInfoToJson_anonymous_tag_recycled(void);
void SerializeQueryInfoToJson_anonymous_pair_recycled(void);
void SerializeQueryInfoToJson_anonymous_component_recycled(void);
void SerializeQueryInfoToJson_serialize_plan_trivial_query(void);
void SerializeQueryInfoToJson_serialize_plan_nontrivial_query(void);

// Testsuite 'MetaUtils'
void MetaUtils_struct_w_2_i32(void);
Expand Down Expand Up @@ -4235,6 +4237,14 @@ bake_test_case SerializeQueryInfoToJson_testcases[] = {
{
"anonymous_component_recycled",
SerializeQueryInfoToJson_anonymous_component_recycled
},
{
"serialize_plan_trivial_query",
SerializeQueryInfoToJson_serialize_plan_trivial_query
},
{
"serialize_plan_nontrivial_query",
SerializeQueryInfoToJson_serialize_plan_nontrivial_query
}
};

Expand Down Expand Up @@ -4681,7 +4691,7 @@ static bake_test_suite suites[] = {
"SerializeQueryInfoToJson",
NULL,
NULL,
25,
27,
SerializeQueryInfoToJson_testcases
},
{
Expand Down

0 comments on commit 5414ea5

Please sign in to comment.