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>
This commit is contained in:
copilot-swe-agent[bot] 2026-02-18 09:02:22 +00:00
parent 21088fe86a
commit 5d145cc80f
3 changed files with 11 additions and 23 deletions

View file

@ -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 避免缓冲区刷新等操作