diff --git a/src/cmt_mpack_utils.c b/src/cmt_mpack_utils.c index e303554..2f16bc3 100644 --- a/src/cmt_mpack_utils.c +++ b/src/cmt_mpack_utils.c @@ -78,6 +78,8 @@ int cmt_mpack_consume_string_tag(mpack_reader_t *reader, cfl_sds_t *output_buffe { uint32_t string_length; mpack_tag_t tag; + cfl_sds_t left_buffer; + uint32_t left_length; if (NULL == output_buffer) { return CMT_MPACK_INVALID_ARGUMENT_ERROR; @@ -105,25 +107,35 @@ int cmt_mpack_consume_string_tag(mpack_reader_t *reader, cfl_sds_t *output_buffe */ if (CMT_MPACK_MAX_STRING_LENGTH < string_length) { - return CMT_MPACK_CORRUPT_INPUT_DATA_ERROR; - } + *output_buffer = cfl_sds_create_size(CMT_MPACK_MAX_STRING_LENGTH + 4); - *output_buffer = cfl_sds_create_size(string_length + 1); + if (NULL == *output_buffer) { + return CMT_MPACK_ALLOCATION_ERROR; + } - if (NULL == *output_buffer) { - return CMT_MPACK_ALLOCATION_ERROR; - } + left_length = string_length - CMT_MPACK_MAX_STRING_LENGTH; + left_buffer = cfl_sds_create_size(left_length + 1); + if (NULL == left_buffer) { + return CMT_MPACK_ALLOCATION_ERROR; + } - cfl_sds_set_len(*output_buffer, string_length); + cfl_sds_set_len(*output_buffer, CMT_MPACK_MAX_STRING_LENGTH); + mpack_read_cstr(reader, *output_buffer, CMT_MPACK_MAX_STRING_LENGTH + 1, CMT_MPACK_MAX_STRING_LENGTH); + cfl_sds_cat_safe(output_buffer, "...", 3); - mpack_read_cstr(reader, *output_buffer, string_length + 1, string_length); + mpack_read_cstr(reader, left_buffer, left_length + 1, left_length); + cfl_sds_destroy(left_buffer); + } + else { + *output_buffer = cfl_sds_create_size(string_length + 1); - if (mpack_ok != mpack_reader_error(reader)) { - cfl_sds_destroy(*output_buffer); + if (NULL == *output_buffer) { + return CMT_MPACK_ALLOCATION_ERROR; + } - *output_buffer = NULL; + cfl_sds_set_len(*output_buffer, string_length); - return CMT_MPACK_ENGINE_ERROR; + mpack_read_cstr(reader, *output_buffer, string_length + 1, string_length); } mpack_done_str(reader);