mirror of
https://github.com/RubyMetric/chsrc
synced 2025-11-18 23:57:43 +08:00
fix(lib): 优化字符串前缀删除逻辑并修复内存泄漏
重构 `xy_str_delete_prefix` 函数以避免不必要的内存分配和释放, 直接使用原字符串进行处理。同时修复 `xy_dir_exist` 函数中 system 调用后未释放临时命令字符串的内存泄漏问题。
This commit is contained in:
19
lib/xy.h
19
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;
|
||||
|
||||
Reference in New Issue
Block a user