Skip to content

Commit

Permalink
fix: memory leak due to memset 0 without destructor
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxilin committed Oct 23, 2024
1 parent c2535de commit 207a390
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 48 deletions.
3 changes: 1 addition & 2 deletions core/include/ten_utils/value/value_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ TEN_UTILS_API ten_value_t *ten_value_from_json(ten_json_t *json);

TEN_UTILS_API ten_json_t *ten_value_to_json(ten_value_t *self);

TEN_UTILS_API bool ten_value_init_from_json(ten_value_t *self,
ten_json_t *json);
TEN_UTILS_API bool ten_value_set_from_json(ten_value_t *self, ten_json_t *json);
2 changes: 2 additions & 0 deletions core/include/ten_utils/value/value_kv.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ TEN_UTILS_API ten_value_kv_t *ten_value_kv_create(const char *name,

TEN_UTILS_API void ten_value_kv_destroy(ten_value_kv_t *self);

TEN_UTILS_API void ten_value_kv_destroy_xxx(ten_value_kv_t *self);

TEN_UTILS_API ten_value_kv_t *ten_value_kv_clone(ten_value_kv_t *target);

TEN_UTILS_API ten_string_t *ten_value_kv_get_key(ten_value_kv_t *self);
Expand Down
15 changes: 14 additions & 1 deletion core/include_internal/ten_utils/value/value_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
//
#pragma once

#include "ten_utils/ten_config.h"

#include <stdbool.h>

#include "ten_utils/ten_config.h"
#include "ten_utils/value/value.h"

TEN_UTILS_API bool ten_value_set_int64(ten_value_t *self, int64_t value);
Expand All @@ -28,3 +29,15 @@ TEN_UTILS_API bool ten_value_set_uint16(ten_value_t *self, uint16_t value);
TEN_UTILS_API bool ten_value_set_uint8(ten_value_t *self, uint8_t value);

TEN_UTILS_API bool ten_value_set_bool(ten_value_t *self, bool value);

TEN_UTILS_API bool ten_value_set_float32(ten_value_t *self, float value);

TEN_UTILS_API bool ten_value_set_float64(ten_value_t *self, double value);

TEN_UTILS_API bool ten_value_set_string(ten_value_t *self, const char *value);

TEN_UTILS_API bool ten_value_set_array_with_move(ten_value_t *self,
ten_list_t *value);

TEN_UTILS_API bool ten_value_set_object_with_move(ten_value_t *self,
ten_list_t *value);
44 changes: 26 additions & 18 deletions core/src/ten_runtime/common/loc.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ ten_value_t *ten_loc_to_value(ten_loc_t *self) {
ten_value_kv_create(
TEN_STR_APP,
ten_value_create_string(ten_string_get_raw_str(&self->app_uri))),
(ten_ptr_listnode_destroy_func_t)ten_value_kv_destroy);
(ten_ptr_listnode_destroy_func_t)ten_value_kv_destroy_xxx);
}

if (!ten_string_is_empty(&self->graph_id)) {
Expand All @@ -271,7 +271,7 @@ ten_value_t *ten_loc_to_value(ten_loc_t *self) {
ten_value_kv_create(
TEN_STR_GRAPH,
ten_value_create_string(ten_string_get_raw_str(&self->graph_id))),
(ten_ptr_listnode_destroy_func_t)ten_value_kv_destroy);
(ten_ptr_listnode_destroy_func_t)ten_value_kv_destroy_xxx);
}

if (!ten_string_is_empty(&self->extension_group_name)) {
Expand All @@ -280,7 +280,7 @@ ten_value_t *ten_loc_to_value(ten_loc_t *self) {
ten_value_kv_create(TEN_STR_EXTENSION_GROUP,
ten_value_create_string(ten_string_get_raw_str(
&self->extension_group_name))),
(ten_ptr_listnode_destroy_func_t)ten_value_kv_destroy);
(ten_ptr_listnode_destroy_func_t)ten_value_kv_destroy_xxx);
}

if (!ten_string_is_empty(&self->extension_name)) {
Expand All @@ -289,7 +289,7 @@ ten_value_t *ten_loc_to_value(ten_loc_t *self) {
ten_value_kv_create(TEN_STR_EXTENSION,
ten_value_create_string(
ten_string_get_raw_str(&self->extension_name))),
(ten_ptr_listnode_destroy_func_t)ten_value_kv_destroy);
(ten_ptr_listnode_destroy_func_t)ten_value_kv_destroy_xxx);
}

ten_value_t *loc_value = ten_value_create_object_with_move(&loc_fields);
Expand Down Expand Up @@ -376,12 +376,16 @@ void ten_loc_init_from_value(ten_loc_t *self, ten_value_t *value) {

if (app_value) {
const char *app_str = ten_value_peek_raw_str(app_value);
ten_string_init_from_c_str(&self->app_uri, app_str, strlen(app_str));
if (app_str && strlen(app_str) > 0) {
ten_string_init_from_c_str(&self->app_uri, app_str, strlen(app_str));
}
}

if (graph_value) {
const char *graph_str = ten_value_peek_raw_str(graph_value);
ten_string_init_from_c_str(&self->graph_id, graph_str, strlen(graph_str));
if (graph_str && strlen(graph_str) > 0) {
ten_string_init_from_c_str(&self->graph_id, graph_str, strlen(graph_str));
}
}

if (extension_group_value) {
Expand All @@ -393,15 +397,17 @@ void ten_loc_init_from_value(ten_loc_t *self, ten_value_t *value) {

const char *group_name_str =
ten_value_peek_raw_str(extension_group_name_value);

ten_string_init_from_c_str(&self->extension_group_name, group_name_str,
strlen(group_name_str));
if (group_name_str && strlen(group_name_str) > 0) {
ten_string_init_from_c_str(&self->extension_group_name, group_name_str,
strlen(group_name_str));
}
} else if (ten_value_is_string(extension_group_value)) {
const char *group_name_str =
ten_value_peek_raw_str(extension_group_value);

ten_string_init_from_c_str(&self->extension_group_name, group_name_str,
strlen(group_name_str));
if (group_name_str && strlen(group_name_str) > 0) {
ten_string_init_from_c_str(&self->extension_group_name, group_name_str,
strlen(group_name_str));
}
} else {
TEN_ASSERT(0, "extension_group must be an object or a string.");
}
Expand All @@ -416,14 +422,16 @@ void ten_loc_init_from_value(ten_loc_t *self, ten_value_t *value) {

const char *extension_name_str =
ten_value_peek_raw_str(extension_name_value);

ten_string_init_from_c_str(&self->extension_name, extension_name_str,
strlen(extension_name_str));
if (extension_name_str && strlen(extension_name_str) > 0) {
ten_string_init_from_c_str(&self->extension_name, extension_name_str,
strlen(extension_name_str));
}
} else if (ten_value_is_string(extension_value)) {
const char *extension_name_str = ten_value_peek_raw_str(extension_value);

ten_string_init_from_c_str(&self->extension_name, extension_name_str,
strlen(extension_name_str));
if (extension_name_str && strlen(extension_name_str) > 0) {
ten_string_init_from_c_str(&self->extension_name, extension_name_str,
strlen(extension_name_str));
}
} else {
TEN_ASSERT(0, "extension must be an object or a string.");
}
Expand Down
52 changes: 27 additions & 25 deletions core/src/ten_runtime/msg/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,9 @@ static bool ten_raw_msg_get_one_field_from_json(
return true;
}

ten_value_init_from_json(field->field_value, json);
if (!ten_value_set_from_json(field->field_value, json)) {
return false;
}
} else {
TEN_ASSERT(ten_value_is_object(field->field_value), "Should not happen.");

Expand Down Expand Up @@ -626,20 +628,20 @@ bool ten_raw_msg_get_field_from_json(ten_msg_t *self, ten_json_t *json,
ten_error_t *err) {
TEN_ASSERT(self && json, "Should not happen.");

for (size_t i = 0; i < ten_msg_fields_info_size; ++i) {
ten_msg_get_field_from_json_func_t get_field_from_json =
ten_msg_fields_info[i].get_field_from_json;
if (get_field_from_json) {
if (!get_field_from_json(self, json, err)) {
return false;
}
}
}
// for (size_t i = 0; i < ten_msg_fields_info_size; ++i) {
// ten_msg_get_field_from_json_func_t get_field_from_json =
// ten_msg_fields_info[i].get_field_from_json;
// if (get_field_from_json) {
// if (!get_field_from_json(self, json, err)) {
// return false;
// }
// }
// }

return true;
// return true;

// return ten_raw_msg_process_field(self, ten_raw_msg_get_one_field_from_json,
// json, err);
return ten_raw_msg_process_field(self, ten_raw_msg_get_one_field_from_json,
json, err);
}

static bool ten_raw_msg_put_one_field_to_json(
Expand Down Expand Up @@ -677,20 +679,20 @@ bool ten_raw_msg_put_field_to_json(ten_msg_t *self, ten_json_t *json,
TEN_ASSERT(self && ten_raw_msg_check_integrity(self) && json,
"Should not happen.");

// return ten_raw_msg_process_field(self, ten_raw_msg_put_one_field_to_json,
// json, err);
return ten_raw_msg_process_field(self, ten_raw_msg_put_one_field_to_json,
json, err);

for (size_t i = 0; i < ten_msg_fields_info_size; ++i) {
ten_msg_put_field_to_json_func_t put_field_to_json =
ten_msg_fields_info[i].put_field_to_json;
if (put_field_to_json) {
if (!put_field_to_json(self, json, err)) {
return false;
}
}
}
// for (size_t i = 0; i < ten_msg_fields_info_size; ++i) {
// ten_msg_put_field_to_json_func_t put_field_to_json =
// ten_msg_fields_info[i].put_field_to_json;
// if (put_field_to_json) {
// if (!put_field_to_json(self, json, err)) {
// return false;
// }
// }
// }

return true;
// return true;
}

bool ten_raw_msg_process_field(ten_msg_t *self,
Expand Down
130 changes: 128 additions & 2 deletions core/src/ten_utils/value/value_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,143 @@
#include <stdint.h>
#include <stdlib.h>

#include "ten_utils/macro/check.h"
#include "include_internal/ten_utils/value/value_set.h"
#include "ten_utils/container/list.h"
#include "ten_utils/lib/json.h"
#include "ten_utils/lib/string.h"
#include "ten_utils/log/log.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/value/type.h"
#include "ten_utils/value/value.h"
#include "ten_utils/value/value_get.h"
#include "ten_utils/value/value_is.h"
#include "ten_utils/value/value_kv.h"

bool ten_value_init_from_json(ten_value_t *self, ten_json_t *json) {
bool ten_value_set_from_json(ten_value_t *self, ten_json_t *json) {
TEN_ASSERT(self && json, "Invalid argument.");

if (!self || !json) {
return false;
}

switch (ten_value_get_type(self)) {
case TEN_TYPE_INVALID:
case TEN_TYPE_NULL:
return false;
case TEN_TYPE_BOOL:
if (ten_json_is_boolean(json)) {
return ten_value_set_bool(self, ten_json_get_boolean_value(json));
}
case TEN_TYPE_INT8:
if (ten_json_is_integer(json) &&
ten_json_get_integer_value(json) >= INT8_MIN &&
ten_json_get_integer_value(json) <= INT8_MAX) {
return ten_value_set_int8(self,
(int8_t)ten_json_get_integer_value(json));
}
case TEN_TYPE_INT16:
if (ten_json_is_integer(json) &&
ten_json_get_integer_value(json) >= INT16_MIN &&
ten_json_get_integer_value(json) <= INT16_MAX) {
return ten_value_set_int16(self,
(int16_t)ten_json_get_integer_value(json));
}
case TEN_TYPE_INT32:
if (ten_json_is_integer(json) &&
ten_json_get_integer_value(json) >= INT32_MIN &&
ten_json_get_integer_value(json) <= INT32_MAX) {
return ten_value_set_int32(self,
(int32_t)ten_json_get_integer_value(json));
}
case TEN_TYPE_INT64:
if (ten_json_is_integer(json)) {
return ten_value_set_int64(self, ten_json_get_integer_value(json));
}
case TEN_TYPE_UINT8:
if (ten_json_is_integer(json) && ten_json_get_integer_value(json) >= 0 &&
ten_json_get_integer_value(json) <= UINT8_MAX) {
return ten_value_set_uint8(self,
(uint8_t)ten_json_get_integer_value(json));
}
case TEN_TYPE_UINT16:
if (ten_json_is_integer(json) && ten_json_get_integer_value(json) >= 0 &&
ten_json_get_integer_value(json) <= UINT16_MAX) {
return ten_value_set_uint16(self,
(uint16_t)ten_json_get_integer_value(json));
}
case TEN_TYPE_UINT32:
if (ten_json_is_integer(json) && ten_json_get_integer_value(json) >= 0 &&
ten_json_get_integer_value(json) <= UINT32_MAX) {
return ten_value_set_uint32(self,
(uint32_t)ten_json_get_integer_value(json));
}
case TEN_TYPE_UINT64:
if (ten_json_is_integer(json) && ten_json_get_integer_value(json) >= 0) {
return ten_value_set_uint64(self,
(uint64_t)ten_json_get_integer_value(json));
}
case TEN_TYPE_FLOAT32:
if (ten_json_is_real(json)) {
return ten_value_set_float32(self,
(float)ten_json_get_real_value(json));
}
case TEN_TYPE_FLOAT64:
if (ten_json_is_real(json)) {
return ten_value_set_float64(self, ten_json_get_real_value(json));
}
case TEN_TYPE_STRING:
if (ten_json_is_string(json)) {
return ten_value_set_string(self, ten_json_peek_string_value(json));
}
case TEN_TYPE_ARRAY:
if (ten_json_is_array(json)) {
// Loop each item in the JSON array and convert them to ten_value_t.
ten_list_t array = TEN_LIST_INIT_VAL;
size_t i = 0;
ten_json_t *item_json = NULL;
ten_json_array_foreach(json, i, item_json) {
ten_value_t *item = ten_value_from_json(item_json);
TEN_ASSERT(item && ten_value_check_integrity(item),
"Invalid argument.");

if (item == NULL) {
// Something wrong, we should return false.
return false;
}

ten_list_push_ptr_back(
&array, item, (ten_ptr_listnode_destroy_func_t)ten_value_destroy);
}

return ten_value_set_array_with_move(self, &array);
}
case TEN_TYPE_OBJECT:
if (ten_json_is_object(json)) {
// Loop each item in the JSON object and convert them to ten_value_kv_t.
ten_list_t object = TEN_LIST_INIT_VAL;
const char *key = NULL;
ten_json_t *value_json = NULL;
ten_json_object_foreach(json, key, value_json) {
ten_value_kv_t *kv = ten_value_kv_from_json(key, value_json);
TEN_ASSERT(kv && ten_value_kv_check_integrity(kv), "Invalid argument.");

ten_list_push_ptr_back(
&object, kv,
(ten_ptr_listnode_destroy_func_t)ten_value_kv_destroy);
}

return ten_value_set_object_with_move(self, &object);
}
case TEN_TYPE_PTR:
case TEN_TYPE_BUF:
TEN_LOGE("Not implemented yet.");
break;
}

return false;
}

static bool ten_value_init_from_json(ten_value_t *self, ten_json_t *json) {
TEN_ASSERT(self && json, "Invalid argument.");

if (!self || !json) {
Expand Down
10 changes: 10 additions & 0 deletions core/src/ten_utils/value/value_kv.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ void ten_value_kv_destroy(ten_value_kv_t *self) {
ten_free(self);
}

void ten_value_kv_destroy_xxx(ten_value_kv_t *self) {
TEN_ASSERT(self && ten_value_kv_check_integrity(self), "Invalid argument.");

ten_string_deinit(&self->key);
if (self->value) {
ten_value_destroy(self->value);
}
ten_free(self);
}

ten_value_kv_t *ten_value_kv_clone(ten_value_kv_t *target) {
TEN_ASSERT(target && ten_value_kv_check_integrity(target),
"Invalid argument.");
Expand Down
Loading

0 comments on commit 207a390

Please sign in to comment.