mirror of
https://github.com/RubyMetric/chsrc
synced 2026-02-02 15:50:29 +08:00
修复 xy_dir_exist 的内存泄露
This commit is contained in:
21
lib/xy.h
21
lib/xy.h
@@ -1186,12 +1186,14 @@ xy_file_exist (const char *path)
|
|||||||
static bool
|
static bool
|
||||||
xy_dir_exist (const char *path)
|
xy_dir_exist (const char *path)
|
||||||
{
|
{
|
||||||
|
char *allocated_dir = NULL;
|
||||||
const char *dir = path;
|
const char *dir = path;
|
||||||
if (xy.on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
if (xy_str_start_with (path, "~"))
|
if (xy_str_start_with (path, "~"))
|
||||||
{
|
{
|
||||||
dir = xy_2strcat (xy_os_home, path + 1);
|
allocated_dir = xy_2strcat (xy_os_home, path + 1);
|
||||||
|
dir = allocated_dir;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1201,29 +1203,30 @@ xy_dir_exist (const char *path)
|
|||||||
// 也可以用 opendir() #include <dirent.h>
|
// 也可以用 opendir() #include <dirent.h>
|
||||||
DWORD attr = GetFileAttributesA (dir);
|
DWORD attr = GetFileAttributesA (dir);
|
||||||
|
|
||||||
|
bool result = false;
|
||||||
if (attr == INVALID_FILE_ATTRIBUTES)
|
if (attr == INVALID_FILE_ATTRIBUTES)
|
||||||
{
|
{
|
||||||
// Q: 我们应该报错吗?
|
// Q: 我们应该报错吗?
|
||||||
return false;
|
result = false;
|
||||||
}
|
}
|
||||||
else if (attr & FILE_ATTRIBUTE_DIRECTORY)
|
else if (attr & FILE_ATTRIBUTE_DIRECTORY)
|
||||||
{
|
{
|
||||||
return true;
|
result = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return false;
|
result = false;
|
||||||
}
|
}
|
||||||
|
if (allocated_dir) free (allocated_dir);
|
||||||
|
return result;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int status = system (xy_2strcat ("test -d ", dir));
|
int status = system (xy_2strcat ("test -d ", dir));
|
||||||
|
bool result = (0==status);
|
||||||
if (0==status)
|
if (allocated_dir) free (allocated_dir);
|
||||||
return true;
|
return result;
|
||||||
else
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user