Skip to content

Commit

Permalink
fix: fix ten_string_append issue (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Nov 7, 2024
1 parent 65c7135 commit 2bab063
Show file tree
Hide file tree
Showing 37 changed files with 284 additions and 155 deletions.
6 changes: 3 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_utils_unit_test",
"cwd": "${workspaceFolder}/out/linux/x64",
"args": [
"--gtest_filter=ThreadTest.negative"
"--gtest_filter=PathTest.positive"
],
"env": {
"LD_LIBRARY_PATH": "${workspaceFolder}/out/linux/x64/tests/standalone/",
Expand Down Expand Up @@ -137,13 +137,13 @@
"request": "launch",
"program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_runtime_smoke_test",
"args": [
"--gtest_filter=CmdConversionTest.CmdConversionConnectCmd"
"--gtest_filter=ExtensionTest.OneEngineConcurrent"
],
"cwd": "${workspaceFolder}/out/linux/x64/tests/standalone/",
"env": {
"LD_LIBRARY_PATH": "${workspaceFolder}/out/linux/x64/tests/standalone/",
"ASAN_OPTIONS": "abort_on_error=1",
// "TEN_ENABLE_MEMORY_TRACKING": "true"
"TEN_ENABLE_MEMORY_TRACKING": "true"
},
},
{
Expand Down
55 changes: 38 additions & 17 deletions core/include/ten_utils/lib/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@
#include "ten_utils/macro/check.h"

#define TEN_STRING_SIGNATURE 0x178445C0402E320DU
#define TEN_STRING_PRE_BUF_SIZE 512

#if defined(NDEBUG)
#define TEN_STRING_PRE_BUF_SIZE 256
#else
// In debug mode, significantly reduce the size of `prebuf` so that
// `ten_string_reserve` is actually triggered. This way, we can test that even
// if `malloc` occurs within `ten_string_reserve`, there will be no memory leak.
#define TEN_STRING_PRE_BUF_SIZE 8
#endif

typedef struct ten_list_t ten_list_t;

Expand All @@ -36,6 +44,13 @@ inline bool ten_string_check_integrity(const ten_string_t *self) {
if (ten_signature_get(&self->signature) != TEN_STRING_SIGNATURE) {
return false;
}

// A normal `ten_string_t`'s `buf` should be a non-NULL value, either pointing
// to `prebuf` or to memory allocated by `malloc`.
if (self->buf == NULL) {
return false;
}

return true;
}

Expand Down Expand Up @@ -65,6 +80,13 @@ TEN_UTILS_API ten_string_t *ten_string_create_from_va_list(const char *fmt,
*/
TEN_UTILS_API ten_string_t *ten_string_clone(const ten_string_t *other);

/**
* @brief Initialize a string object from another string object.
* @param self The string object.
* @param other The other string object.
*/
TEN_UTILS_API void ten_string_copy(ten_string_t *self, ten_string_t *other);

/**
* @brief Initialize a string object from existing memory.
* @param self The string object.
Expand All @@ -79,12 +101,8 @@ TEN_UTILS_API void ten_string_init(ten_string_t *self);
TEN_UTILS_API void ten_string_init_formatted(ten_string_t *self,
const char *fmt, ...);

/**
* @brief Initialize a string object from another string object.
* @param self The string object.
* @param other The other string object.
*/
TEN_UTILS_API void ten_string_copy(ten_string_t *self, ten_string_t *other);
TEN_UTILS_API void ten_string_init_from_string(ten_string_t *self,
ten_string_t *other);

/**
* @brief Initialize a string object from another string object.
Expand All @@ -93,7 +111,7 @@ TEN_UTILS_API void ten_string_copy(ten_string_t *self, ten_string_t *other);
* @param size the max size, copy all if size <= 0
*/
TEN_UTILS_API void ten_string_init_from_c_str(ten_string_t *self,
const char *other, size_t size);
const char *str, size_t size);

/**
* @brief Destroy a string object and release the memory.
Expand All @@ -120,16 +138,16 @@ TEN_UTILS_API void ten_string_clear(ten_string_t *self);
*/
TEN_UTILS_API void ten_string_reserve(ten_string_t *self, size_t extra);

TEN_UTILS_API void ten_string_append_from_va_list(ten_string_t *self,
const char *fmt, va_list ap);

/**
* @brief Set the string object with a c string.
* @brief Append a c string to the string object.
* @param self The string object.
* @param fmt The c string.
*/
TEN_UTILS_API void ten_string_set_formatted(ten_string_t *self, const char *fmt,
...);
TEN_UTILS_API void ten_string_append_formatted(ten_string_t *self,
const char *fmt, ...);

TEN_UTILS_API void ten_string_append_from_va_list(ten_string_t *self,
const char *fmt, va_list ap);

/**
* @brief Prepend a c string to the string object.
Expand All @@ -143,12 +161,15 @@ TEN_UTILS_API void ten_string_prepend_from_va_list(ten_string_t *self,
const char *fmt, va_list ap);

/**
* @brief Append a c string to the string object.
* @brief Set the string object with a c string.
* @param self The string object.
* @param fmt The c string.
*/
TEN_UTILS_API void ten_string_append_formatted(ten_string_t *self,
const char *fmt, ...);
TEN_UTILS_API void ten_string_set_formatted(ten_string_t *self, const char *fmt,
...);

TEN_UTILS_API void ten_string_set_from_c_str(ten_string_t *self,
const char *str, size_t size);

/**
* @brief Check if the string object is empty.
Expand Down
8 changes: 6 additions & 2 deletions core/include/ten_utils/sanitizer/memory_check.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
typedef struct ten_sanitizer_memory_record_t {
void *addr;
size_t size;
ten_string_t func_name;
ten_string_t file_name;

// Do not use `ten_string_t` here to avoid a circular dependency between
// `ten_string_t` and `ten_malloc`.
char *func_name;
char *file_name;

uint32_t lineno;
} ten_sanitizer_memory_record_t;

Expand Down
51 changes: 31 additions & 20 deletions core/include_internal/ten_runtime/common/loc.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,49 @@ TEN_RUNTIME_PRIVATE_API bool ten_loc_check_integrity(ten_loc_t *self);

TEN_RUNTIME_PRIVATE_API ten_loc_t *ten_loc_create_empty(void);

TEN_RUNTIME_API ten_loc_t *ten_loc_create(const char *app_uri,
const char *graph_id,
const char *extension_group_name,
const char *extension_name);

TEN_RUNTIME_PRIVATE_API ten_loc_t *ten_loc_create_from_value(
ten_value_t *value);

TEN_RUNTIME_API void ten_loc_destroy(ten_loc_t *self);

TEN_RUNTIME_PRIVATE_API ten_loc_t *ten_loc_clone(ten_loc_t *src);

TEN_RUNTIME_PRIVATE_API void ten_loc_copy(ten_loc_t *self, ten_loc_t *src);

TEN_RUNTIME_PRIVATE_API void ten_loc_init_empty(ten_loc_t *self);

TEN_RUNTIME_PRIVATE_API void ten_loc_init(ten_loc_t *self, const char *app_uri,
const char *graph_id,
const char *extension_group_name,
const char *extension_name);

TEN_RUNTIME_PRIVATE_API void ten_loc_init_from_loc(ten_loc_t *self,
ten_loc_t *src);

TEN_RUNTIME_PRIVATE_API void ten_loc_init_from_value(ten_loc_t *self,
ten_value_t *value);

TEN_RUNTIME_PRIVATE_API void ten_loc_init_from_json(ten_loc_t *self,
ten_json_t *json);

TEN_RUNTIME_PRIVATE_API void ten_loc_deinit(ten_loc_t *self);

TEN_RUNTIME_PRIVATE_API void ten_loc_set(ten_loc_t *self, const char *app_uri,
const char *graph_id,
const char *extension_group_name,
const char *extension_name);

TEN_RUNTIME_PRIVATE_API void ten_loc_set_from_loc(ten_loc_t *self,
ten_loc_t *src);

TEN_RUNTIME_PRIVATE_API void ten_loc_set_from_value(ten_loc_t *self,
ten_value_t *value);

TEN_RUNTIME_PRIVATE_API bool ten_loc_is_empty(ten_loc_t *self);

TEN_RUNTIME_PRIVATE_API void ten_loc_clear(ten_loc_t *self);
Expand All @@ -79,27 +107,10 @@ TEN_RUNTIME_PRIVATE_API void ten_loc_to_string(ten_loc_t *self,

TEN_RUNTIME_PRIVATE_API ten_json_t *ten_loc_to_json(ten_loc_t *self);

TEN_RUNTIME_PRIVATE_API void ten_loc_to_json_string(ten_loc_t *self,
ten_string_t *result);

TEN_RUNTIME_PRIVATE_API bool ten_loc_set_value(ten_loc_t *self,
ten_value_t *value);

TEN_RUNTIME_PRIVATE_API ten_value_t *ten_loc_to_value(ten_loc_t *self);

TEN_RUNTIME_API ten_loc_t *ten_loc_create(const char *app_uri,
const char *graph_id,
const char *extension_group_name,
const char *extension_name);

TEN_RUNTIME_PRIVATE_API ten_loc_t *ten_loc_create_from_value(
ten_value_t *value);

TEN_RUNTIME_PRIVATE_API void ten_loc_init_empty(ten_loc_t *self);

TEN_RUNTIME_PRIVATE_API void ten_loc_init_from_json(ten_loc_t *self,
ten_json_t *json);

TEN_RUNTIME_API void ten_loc_destroy(ten_loc_t *self);

TEN_RUNTIME_PRIVATE_API void ten_loc_deinit(ten_loc_t *self);

TEN_RUNTIME_PRIVATE_API void ten_loc_to_json_string(ten_loc_t *self,
ten_string_t *result);
5 changes: 4 additions & 1 deletion core/include_internal/ten_utils/value/value_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ 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_string(ten_value_t *self, const char *str);

TEN_UTILS_API bool ten_value_set_string_with_size(ten_value_t *self,
const char *str, size_t len);

TEN_UTILS_API bool ten_value_set_string_with_size(ten_value_t *self,
const char *value,
Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_runtime/addon/addon.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ void ten_addon_find_and_set_base_dir(ten_addon_host_t *self,
// If the addon's base dir cannot be found by searching upward through the
// parent folders, simply trust the passed-in parameter as the addon’s base
// dir.
ten_string_init_from_c_str(&self->base_dir, start_path, strlen(start_path));
ten_string_set_from_c_str(&self->base_dir, start_path, strlen(start_path));
}
}

Expand Down
1 change: 1 addition & 0 deletions core/src/ten_runtime/addon/addon_autoload.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ bool ten_addon_load_all_from_app_base_dir(ten_app_t *app, ten_error_t *err) {

for (int i = 0; i < sizeof(folders) / sizeof(folders[0]); i++) {
ten_string_t module_path;
ten_string_init(&module_path);
ten_string_copy(&module_path, &app->base_dir);

do {
Expand Down
4 changes: 2 additions & 2 deletions core/src/ten_runtime/addon/ten_env/on_xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ void ten_addon_on_init_done(ten_env_t *self) {
// runtime would use that name instead of the name specified in the codes to
// register it to the extension store.
if (strlen(manifest_name)) {
ten_string_init_from_c_str(&addon_host->name, manifest_name,
strlen(manifest_name));
ten_string_set_from_c_str(&addon_host->name, manifest_name,
strlen(manifest_name));
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_runtime/app/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ bool ten_app_init_uri(ten_app_t *self, ten_value_t *value) {
? ten_value_peek_raw_str(value)
: ten_string_get_raw_str(&default_url);

ten_string_init_from_c_str(&self->uri, url_str, strlen(url_str));
ten_string_set_from_c_str(&self->uri, url_str, strlen(url_str));

ten_string_deinit(&default_url);

Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_runtime/app/predefined_graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ bool ten_app_get_predefined_graphs_from_property(ten_app_t *self) {
result = false;
goto done;
}
ten_string_init_from_c_str(
ten_string_set_from_c_str(
&predefined_graph_info->name,
ten_value_peek_raw_str(predefined_graph_info_name_value),
strlen(ten_value_peek_raw_str(predefined_graph_info_name_value)));
Expand Down
4 changes: 2 additions & 2 deletions core/src/ten_runtime/app/ten_env/on_xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ void ten_app_on_configure_done(ten_env_t *ten_env) {
ten_app_adjust_and_validate_property_on_configure_done(self);

if (ten_string_is_empty(&self->uri)) {
ten_string_init_from_c_str(&self->uri, TEN_STR_LOCALHOST,
strlen(TEN_STR_LOCALHOST));
ten_string_set_from_c_str(&self->uri, TEN_STR_LOCALHOST,
strlen(TEN_STR_LOCALHOST));
}

ten_addon_load_all_from_app_base_dir(self, &err);
Expand Down
Loading

0 comments on commit 2bab063

Please sign in to comment.