From 424c8936498636fef69c8d8894956137dda017f2 Mon Sep 17 00:00:00 2001 From: Nick Drabsch Date: Fri, 30 Jun 2023 16:56:25 +0100 Subject: [PATCH] Don't return malformed JSON from REST API if serialization failed --- flecs.c | 14 ++++++++++++-- src/addons/rest.c | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/flecs.c b/flecs.c index f82d9cf3c..ec102d541 100644 --- a/flecs.c +++ b/flecs.c @@ -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; } @@ -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; } diff --git a/src/addons/rest.c b/src/addons/rest.c index 64ca185eb..411bdb0c7 100644 --- a/src/addons/rest.c +++ b/src/addons/rest.c @@ -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; } @@ -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; }