From 6f312a66c0f436febda356b15abe2ce42d44f53e Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Tue, 7 Oct 2025 18:41:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(lib):=20=E4=BC=98=E5=8C=96=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E5=89=8D=E7=BC=80=E5=88=A0=E9=99=A4=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=B9=B6=E4=BF=AE=E5=A4=8D=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构 `xy_str_delete_prefix` 函数以避免不必要的内存分配和释放, 直接使用原字符串进行处理。同时修复 `xy_dir_exist` 函数中 system 调用后未释放临时命令字符串的内存泄漏问题。 --- lib/xy.h | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index f7d1325..77cd60f 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -581,13 +581,16 @@ xy_str_start_with (const char *str, const char *prefix) static char * xy_str_delete_prefix (const char *str, const char *prefix) { - char *new = xy_strdup (str); bool yes = xy_str_start_with (str, prefix); - if (!yes) return new; - size_t len = strlen (prefix); - char *ret = xy_strdup (new + len); - free (new); - return ret; + if (!yes) + { + return xy_strdup(str); + } + else + { + size_t len = strlen (prefix); + return xy_strdup (str + len); + } } /** @@ -1224,7 +1227,9 @@ xy_dir_exist (const char *path) } else { - int status = system (xy_2strcat ("test -d ", dir)); + char *tmp_cmd = xy_2strcat ("test -d ", dir); + int status = system (tmp_cmd); + free (tmp_cmd); bool result = (0==status); if (allocated_dir) free (allocated_dir); return result;