From 5d145cc80fc681b5ac89a6b5849a4094c9c7c9b9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 18 Feb 2026 09:02:22 +0000 Subject: [PATCH] Simplify code based on final review feedback - Use direct character assignment for constant strings - Remove unnecessary tracking array and cleanup code - Simplify string concatenation logic - Fix length calculation to match actual strncat behavior - Code is cleaner and more maintainable Co-authored-by: squassina <8495707+squassina@users.noreply.github.com> --- src/config/parse_config.h | 16 +++++----------- src/dispatch/bind_define.h | 12 +++--------- src/fetch/common.h | 6 +++--- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/src/config/parse_config.h b/src/config/parse_config.h index cb1cb36f..114e19e8 100644 --- a/src/config/parse_config.h +++ b/src/config/parse_config.h @@ -601,21 +601,15 @@ static char *combine_args_until_empty(char *values[], int count) { size_t current_len = 0; for (int i = 0; i < first_empty; i++) { if (i > 0 && current_len < total_len) { - size_t remaining = total_len - current_len; - size_t to_add = (remaining < 1) ? 0 : 1; - if (to_add > 0) { - strncat(combined, ",", remaining); - current_len += to_add; // We know we added 1 character - } + strncat(combined, ",", total_len - current_len); + current_len++; } if (current_len < total_len) { size_t remaining = total_len - current_len; size_t val_len = strlen(values[i]); - size_t to_add = (val_len < remaining) ? val_len : remaining; - if (to_add > 0) { - strncat(combined, values[i], remaining); - current_len += to_add; - } + size_t will_add = (val_len < remaining) ? val_len : remaining; + strncat(combined, values[i], remaining); + current_len += will_add; } } diff --git a/src/dispatch/bind_define.h b/src/dispatch/bind_define.h index c522ff54..4407f981 100644 --- a/src/dispatch/bind_define.h +++ b/src/dispatch/bind_define.h @@ -836,7 +836,6 @@ int32_t spawn(const Arg *arg) { // 2. 解析参数 char *argv[64]; - bool argv_allocated[64] = {false}; // Track which argv entries were allocated int32_t argc = 0; char *token = strtok((char *)arg->v, " "); @@ -847,12 +846,10 @@ int32_t spawn(const Arg *arg) { argv[argc] = strdup(p.we_wordv[0]); wordfree(&p); // Free immediately after copying if (argv[argc] != NULL) { - argv_allocated[argc] = true; argc++; } } else { argv[argc] = token; - argv_allocated[argc] = false; argc++; } token = strtok(NULL, " "); @@ -862,12 +859,9 @@ int32_t spawn(const Arg *arg) { // 3. 执行命令 execvp(argv[0], argv); - // 4. execvp 失败时:清理分配的字符串并打印错误 - for (int arg_idx = 0; arg_idx < argc; arg_idx++) { - if (argv_allocated[arg_idx]) { - free(argv[arg_idx]); - } - } + // 4. execvp 失败时打印错误并退出 + // Note: We don't need to free here since we're about to _exit + // The OS will clean up when the process exits wlr_log(WLR_ERROR, "mango: execvp '%s' failed: %s\n", argv[0], strerror(errno)); _exit(EXIT_FAILURE); // 使用 _exit 避免缓冲区刷新等操作 diff --git a/src/fetch/common.h b/src/fetch/common.h index 18d90232..b04f090d 100644 --- a/src/fetch/common.h +++ b/src/fetch/common.h @@ -77,9 +77,9 @@ void get_layout_abbr(char *abbr, const char *full_name) { abbr[2] = '\0'; } else { // 5. 最终回退:返回 "xx" - // Explicit null termination for consistency - strncpy(abbr, "xx", LAYOUT_ABBR_SIZE - 1); - abbr[LAYOUT_ABBR_SIZE - 1] = '\0'; + abbr[0] = 'x'; + abbr[1] = 'x'; + abbr[2] = '\0'; } }