Skip to content

Commit 6f312a6

Browse files
author
Mikachu2333
committed
fix(lib): 优化字符串前缀删除逻辑并修复内存泄漏
重构 `xy_str_delete_prefix` 函数以避免不必要的内存分配和释放, 直接使用原字符串进行处理。同时修复 `xy_dir_exist` 函数中 system 调用后未释放临时命令字符串的内存泄漏问题。
1 parent 948d551 commit 6f312a6

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

lib/xy.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -581,13 +581,16 @@ xy_str_start_with (const char *str, const char *prefix)
581581
static char *
582582
xy_str_delete_prefix (const char *str, const char *prefix)
583583
{
584-
char *new = xy_strdup (str);
585584
bool yes = xy_str_start_with (str, prefix);
586-
if (!yes) return new;
587-
size_t len = strlen (prefix);
588-
char *ret = xy_strdup (new + len);
589-
free (new);
590-
return ret;
585+
if (!yes)
586+
{
587+
return xy_strdup(str);
588+
}
589+
else
590+
{
591+
size_t len = strlen (prefix);
592+
return xy_strdup (str + len);
593+
}
591594
}
592595

593596
/**
@@ -1224,7 +1227,9 @@ xy_dir_exist (const char *path)
12241227
}
12251228
else
12261229
{
1227-
int status = system (xy_2strcat ("test -d ", dir));
1230+
char *tmp_cmd = xy_2strcat ("test -d ", dir);
1231+
int status = system (tmp_cmd);
1232+
free (tmp_cmd);
12281233
bool result = (0==status);
12291234
if (allocated_dir) free (allocated_dir);
12301235
return result;

0 commit comments

Comments
 (0)