Skip to content

Commit

Permalink
fix: fix ten_string_append issue (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Nov 7, 2024
1 parent 4886e6f commit aa32951
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@
"request": "launch",
"program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_runtime_smoke_test",
"args": [
"--gtest_filter=-ExtensionTest.*AttemptToSuspend*"
"--gtest_filter=CmdConversionTest.CmdConversionConnectCmd"
],
"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
2 changes: 1 addition & 1 deletion core/include/ten_utils/lib/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "ten_utils/macro/check.h"

#define TEN_STRING_SIGNATURE 0x178445C0402E320DU
#define TEN_STRING_PRE_BUF_SIZE 256
#define TEN_STRING_PRE_BUF_SIZE 512

typedef struct ten_list_t ten_list_t;

Expand Down
52 changes: 45 additions & 7 deletions core/src/ten_utils/lib/sys/general/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,29 @@ ten_string_t *ten_string_create_from_c_str(const char *str, size_t size) {
return result;
}

#define MAX_RETRIES 10

void ten_string_append_from_va_list(ten_string_t *self, const char *fmt,
va_list ap) {
TEN_ASSERT(self && ten_string_check_integrity(self), "Invalid argument.");

size_t retry_count = 0;

va_list cp;
va_copy(cp, ap);

for (;;) {
va_copy(cp, ap);
va_list temp;
va_copy(temp, cp);

int n = vsnprintf(&self->buf[self->first_unused_idx],
self->buf_size - self->first_unused_idx, fmt, cp);
va_end(cp);
self->buf_size - self->first_unused_idx, fmt, temp);

va_end(temp);

if ((n > -1) && ((size_t)n < (self->buf_size - self->first_unused_idx))) {
self->first_unused_idx += n;
va_end(cp);
return;
}

Expand All @@ -61,6 +71,13 @@ void ten_string_append_from_va_list(ten_string_t *self, const char *fmt,
} else {
ten_string_reserve(self, self->buf_size * 2); // 2x
}

if (++retry_count > MAX_RETRIES) {
va_end(cp);

TEN_ASSERT(0, "Should not happen");
return;
}
}
}

Expand Down Expand Up @@ -219,20 +236,41 @@ void ten_string_clear(ten_string_t *self) {
self->buf[0] = 0;
}

#define MAX_BUFFER_SIZE (10 * 1024 * 1024) // 10 M

void ten_string_reserve(ten_string_t *self, size_t extra) {
TEN_ASSERT(self && ten_string_check_integrity(self), "Invalid argument.");
if ((self->buf_size - self->first_unused_idx) < extra) {

if (extra > SIZE_MAX - self->first_unused_idx) {
TEN_ASSERT(0, "Size overflow detected.");
return;
}

size_t required_size = self->first_unused_idx + extra;
if (required_size > (size_t)MAX_BUFFER_SIZE) {
TEN_ASSERT(0, "Buffer size exceeds the maximum limit.");
return;
}

if (self->buf_size < required_size) {
size_t new_size = self->buf_size * 2;
if (new_size < required_size) {
new_size = required_size;
}

char *tmp = NULL;
if (self->buf == self->pre_buf) {
tmp = (char *)TEN_MALLOC(self->buf_size + extra);
tmp = (char *)TEN_MALLOC(new_size);
TEN_ASSERT(tmp, "Failed to allocate memory.");

memcpy(tmp, self->buf, self->first_unused_idx);
} else {
tmp = (char *)TEN_REALLOC(self->buf, self->buf_size + extra);
tmp = (char *)TEN_REALLOC(self->buf, new_size);
TEN_ASSERT(tmp, "Failed to allocate memory.");
}

self->buf = tmp;
self->buf_size += extra;
self->buf_size = new_size;
}
}

Expand Down
10 changes: 6 additions & 4 deletions core/src/ten_utils/log/formatter.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
#include "ten_utils/ten_config.h"

#include <inttypes.h>
#include <time.h>

#include "include_internal/ten_utils/log/level.h"
Expand Down Expand Up @@ -95,8 +96,9 @@ void ten_log_colored_formatter(ten_string_t *buf, TEN_LOG_LEVEL level,
break;
}

ten_string_append_formatted(buf, " %d(%d) %s%c%s", pid, tid, level_color,
ten_log_level_char(level), TEN_LOG_COLOR_RESET);
ten_string_append_formatted(buf, " %" PRId64 "(%" PRId64 ") %s%c%s", pid, tid,
level_color, ten_log_level_char(level),
TEN_LOG_COLOR_RESET);

// Add color to function name.
if (func_name_len) {
Expand All @@ -110,9 +112,9 @@ void ten_log_colored_formatter(ten_string_t *buf, TEN_LOG_LEVEL level,
const char *actual_file_name =
filename(file_name, file_name_len, &actual_file_name_len);
if (actual_file_name_len) {
ten_string_append_formatted(buf, "%s@%.*s:%d%s", TEN_LOG_COLOR_BLUE,
ten_string_append_formatted(buf, "%s@%.*s:%zu%s", TEN_LOG_COLOR_BLUE,
(int)actual_file_name_len, actual_file_name,
(int)line_no, TEN_LOG_COLOR_RESET);
line_no, TEN_LOG_COLOR_RESET);
}

// Add color to message.
Expand Down
3 changes: 2 additions & 1 deletion core/src/ten_utils/log/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ void ten_log_set_output_to_stderr(ten_log_t *self) {
ten_log_output_set(self, ten_log_output_to_stderr_cb, NULL, NULL);

#if defined(OS_LINUX) || defined(OS_MACOS)
ten_log_set_formatter(self, ten_log_colored_formatter, NULL);
// ten_log_set_formatter(self, ten_log_colored_formatter, NULL);
ten_log_set_formatter(self, ten_log_default_formatter, NULL);
#else
ten_log_set_formatter(self, ten_log_default_formatter, NULL);
#endif
Expand Down
9 changes: 7 additions & 2 deletions core/src/ten_utils/sanitizer/memory_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ void ten_sanitizer_memory_record_dump(void) {

ten_sanitizer_memory_record_t *info = ten_ptr_listnode_get(iter.node);

TEN_LOGE("\t#%ld %p(%ld bytes) in %s %s:%d", idx, info->addr, info->size,
TEN_LOGE("\t#%zu %p(%zu bytes) in %s %s:%d", idx, info->addr, info->size,
ten_string_get_raw_str(&info->func_name),
ten_string_get_raw_str(&info->file_name), info->lineno);

Expand Down Expand Up @@ -406,10 +406,15 @@ void *ten_sanitizer_memory_realloc(void *addr, size_t size,
self, size, file_name, lineno, func_name);
if (!record) {
ten_free(self);
ten_sanitizer_memory_record_del(&g_memory_records, addr);
if (addr != NULL) {
ten_sanitizer_memory_record_del(&g_memory_records, addr);
}
return NULL;
}

if (addr != NULL) {
ten_sanitizer_memory_record_del(&g_memory_records, addr);
}
ten_sanitizer_memory_record_add(&g_memory_records, record);
}

Expand Down
1 change: 1 addition & 0 deletions core/src/ten_utils/value/value_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "include_internal/ten_utils/value/value_set.h"

#include "ten_utils/container/list.h"
#include "ten_utils/lib/string.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/value/type.h"
#include "ten_utils/value/value.h"
Expand Down

0 comments on commit aa32951

Please sign in to comment.