From 38fdf917a17485621c9ff907468442fd43d68418 Mon Sep 17 00:00:00 2001 From: Xu Jun <693788454@qq.com> Date: Thu, 23 Jun 2022 15:55:42 +0800 Subject: [PATCH] truncate function name if too long --- core/iwasm/aot/aot_runtime.c | 18 ++++++++++++++---- core/iwasm/interpreter/wasm_runtime.c | 18 ++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index cac7b6f03..d4f0f2fdd 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -3106,6 +3106,7 @@ aot_dump_call_stack(WASMExecEnv *exec_env, bool print, char *buf, uint32 len) while (n < total_frames) { WASMCApiFrame frame = { 0 }; + uint32 line_length, i; if (!bh_vector_get(module_inst->frames.ptr, n, &frame)) { return 0; @@ -3113,12 +3114,21 @@ aot_dump_call_stack(WASMExecEnv *exec_env, bool print, char *buf, uint32 len) /* function name not exported, print number instead */ if (frame.func_name_wp == NULL) { - snprintf(line_buf, sizeof(line_buf), "#%02d $f%d\n", n, - frame.func_index); + line_length = snprintf(line_buf, sizeof(line_buf), "#%02d $f%d\n", + n, frame.func_index); } else { - snprintf(line_buf, sizeof(line_buf), "#%02d %s\n", n, - frame.func_name_wp); + line_length = snprintf(line_buf, sizeof(line_buf), "#%02d %s\n", n, + frame.func_name_wp); + } + + if (line_length >= sizeof(line_buf)) { + uint32 line_buffer_len = sizeof(line_buf); + /* If line too long, ensure the last character is '\n' */ + for (i = line_buffer_len - 5; i < line_buffer_len - 2; i++) { + line_buf[i] = '.'; + } + line_buf[line_buffer_len - 2] = '\n'; } PRINT_OR_DUMP(); diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 026f7353f..2761dcfd7 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -2616,6 +2616,7 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf, while (n < total_frames) { WASMCApiFrame frame = { 0 }; + uint32 line_length, i; if (!bh_vector_get(module_inst->frames, n, &frame)) { return 0; @@ -2623,12 +2624,21 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf, /* function name not exported, print number instead */ if (frame.func_name_wp == NULL) { - snprintf(line_buf, sizeof(line_buf), "#%02d $f%d\n", n, - frame.func_index); + line_length = snprintf(line_buf, sizeof(line_buf), "#%02d $f%d\n", + n, frame.func_index); } else { - snprintf(line_buf, sizeof(line_buf), "#%02d %s\n", n, - frame.func_name_wp); + line_length = snprintf(line_buf, sizeof(line_buf), "#%02d %s\n", n, + frame.func_name_wp); + } + + if (line_length >= sizeof(line_buf)) { + uint32 line_buffer_len = sizeof(line_buf); + /* If line too long, ensure the last character is '\n' */ + for (i = line_buffer_len - 5; i < line_buffer_len - 2; i++) { + line_buf[i] = '.'; + } + line_buf[line_buffer_len - 2] = '\n'; } PRINT_OR_DUMP();