Skip to content

Commit

Permalink
Don't return malformed JSON from REST API if serialization failed
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroErrors authored and SanderMertens committed Jul 9, 2023
1 parent 798f735 commit 424c893
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
14 changes: 12 additions & 2 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -37211,7 +37211,12 @@ bool flecs_rest_reply_entity(

ecs_entity_to_json_desc_t desc = ECS_ENTITY_TO_JSON_INIT;
flecs_rest_parse_json_ser_entity_params(&desc, req);
ecs_entity_to_json_buf(world, e, &reply->body, &desc);
if (ecs_entity_to_json_buf(world, e, &reply->body, &desc) != 0) {
ecs_strbuf_reset(&reply->body);
reply->code = 500;
reply->status = "Internal server error";
return true;
}
return true;
}

Expand All @@ -37222,7 +37227,12 @@ bool flecs_rest_reply_world(
ecs_http_reply_t *reply)
{
(void)req;
ecs_world_to_json_buf(world, &reply->body, NULL);
if (ecs_world_to_json_buf(world, &reply->body, NULL) != 0) {
ecs_strbuf_reset(&reply->body);
reply->code = 500;
reply->status = "Internal server error";
return true;
}
return true;
}

Expand Down
14 changes: 12 additions & 2 deletions src/addons/rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@ bool flecs_rest_reply_entity(

ecs_entity_to_json_desc_t desc = ECS_ENTITY_TO_JSON_INIT;
flecs_rest_parse_json_ser_entity_params(&desc, req);
ecs_entity_to_json_buf(world, e, &reply->body, &desc);
if (ecs_entity_to_json_buf(world, e, &reply->body, &desc) != 0) {
ecs_strbuf_reset(&reply->body);
reply->code = 500;
reply->status = "Internal server error";
return true;
}
return true;
}

Expand All @@ -232,7 +237,12 @@ bool flecs_rest_reply_world(
ecs_http_reply_t *reply)
{
(void)req;
ecs_world_to_json_buf(world, &reply->body, NULL);
if (ecs_world_to_json_buf(world, &reply->body, NULL) != 0) {
ecs_strbuf_reset(&reply->body);
reply->code = 500;
reply->status = "Internal server error";
return true;
}
return true;
}

Expand Down

0 comments on commit 424c893

Please sign in to comment.