fix(lib): 优化字符串前缀删除逻辑并修复内存泄漏

重构 `xy_str_delete_prefix` 函数以避免不必要的内存分配和释放,
直接使用原字符串进行处理。同时修复 `xy_dir_exist` 函数中 system
调用后未释放临时命令字符串的内存泄漏问题。
This commit is contained in:
Mikachu2333 2025-10-07 18:41:15 +08:00
parent 948d55185f
commit 6f312a66c0

View File

@ -581,13 +581,16 @@ xy_str_start_with (const char *str, const char *prefix)
static char * static char *
xy_str_delete_prefix (const char *str, const char *prefix) xy_str_delete_prefix (const char *str, const char *prefix)
{ {
char *new = xy_strdup (str);
bool yes = xy_str_start_with (str, prefix); bool yes = xy_str_start_with (str, prefix);
if (!yes) return new; if (!yes)
size_t len = strlen (prefix); {
char *ret = xy_strdup (new + len); return xy_strdup(str);
free (new); }
return ret; else
{
size_t len = strlen (prefix);
return xy_strdup (str + len);
}
} }
/** /**
@ -1224,7 +1227,9 @@ xy_dir_exist (const char *path)
} }
else 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); bool result = (0==status);
if (allocated_dir) free (allocated_dir); if (allocated_dir) free (allocated_dir);
return result; return result;