mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-06-29 18:15:55 +08:00
fix: 修复JSON导入、备份目录创建和内存安全等问题
修复JSON导入时转义字符处理不完整的问题,添加对\b、\f等控制字符的转义 改进备份目录创建逻辑,使用SHCreateDirectoryExW递归创建目录 修复内存分配失败处理,避免空指针解引用 修正选项卡标题设置位置,从Dialog改为Tabs控件 增强导入功能,支持TXT文件导入时选择目标变量类型 优化清理无效路径算法,使用标记数组减少内存移动 修复宽字符环境变量设置,使用_wputenv_s替代putenv 添加导入数据初始化,防止未初始化内存访问 改进文件属性检查,使用宽字符API支持Unicode路径
This commit is contained in:
+118
-88
@@ -11,25 +11,28 @@
|
||||
static void get_current_datetime(char *buffer, int size)
|
||||
{
|
||||
time_t now = time(NULL);
|
||||
struct tm *tm_info = localtime(&now);
|
||||
strftime(buffer, size, "%Y-%m-%d %H:%M:%S", tm_info);
|
||||
struct tm tm_info;
|
||||
localtime_s(&tm_info, &now);
|
||||
strftime(buffer, size, "%Y-%m-%d %H:%M:%S", &tm_info);
|
||||
}
|
||||
|
||||
// 转义 JSON 字符串中的特殊字符
|
||||
// 转义 JSON 字符串中的特殊字符(符合 RFC 8259 规范)
|
||||
static char *escape_json_string(const char *str)
|
||||
{
|
||||
if (!str)
|
||||
return NULL;
|
||||
|
||||
int len = strlen(str);
|
||||
char *result = (char *)malloc(len * 2 + 1);
|
||||
// 最坏情况:每个字符都需要 \uXXXX 转义(6字节)
|
||||
char *result = (char *)malloc(len * 6 + 1);
|
||||
if (!result)
|
||||
return NULL;
|
||||
|
||||
char *p = result;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
switch (str[i])
|
||||
unsigned char c = (unsigned char)str[i];
|
||||
switch (c)
|
||||
{
|
||||
case '\\':
|
||||
*p++ = '\\';
|
||||
@@ -51,8 +54,24 @@ static char *escape_json_string(const char *str)
|
||||
*p++ = '\\';
|
||||
*p++ = 't';
|
||||
break;
|
||||
case '\b':
|
||||
*p++ = '\\';
|
||||
*p++ = 'b';
|
||||
break;
|
||||
case '\f':
|
||||
*p++ = '\\';
|
||||
*p++ = 'f';
|
||||
break;
|
||||
default:
|
||||
*p++ = str[i];
|
||||
if (c < 0x20) // 其他控制字符 (0x00-0x1F)
|
||||
{
|
||||
sprintf(p, "\\u%04x", c);
|
||||
p += 6;
|
||||
}
|
||||
else
|
||||
{
|
||||
*p++ = str[i];
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -124,6 +143,9 @@ ErrorCode export_paths_to_file(const ExportData *data, const char *filepath)
|
||||
// 移除字符串首尾的空格、制表符、换行符和回车符
|
||||
static void trim_whitespace(char *str)
|
||||
{
|
||||
if (!str || *str == '\0')
|
||||
return;
|
||||
|
||||
char *start = str;
|
||||
while (*start == ' ' || *start == '\t')
|
||||
start++;
|
||||
@@ -152,7 +174,20 @@ static int is_comment_or_empty(const char *line)
|
||||
static int is_json_file(const char *filepath)
|
||||
{
|
||||
const char *ext = strrchr(filepath, '.');
|
||||
return ext && strcasecmp(ext, ".json") == 0;
|
||||
return ext && _stricmp(ext, ".json") == 0;
|
||||
}
|
||||
|
||||
// 检查引号前是否有奇数个连续反斜杠(奇数个表示引号被转义)
|
||||
static int is_quote_escaped(const char *quote_pos, const char *line_start)
|
||||
{
|
||||
int backslash_count = 0;
|
||||
const char *p = quote_pos - 1;
|
||||
while (p >= line_start && *p == '\\')
|
||||
{
|
||||
backslash_count++;
|
||||
p--;
|
||||
}
|
||||
return (backslash_count % 2) == 1; // 奇数个反斜杠表示转义
|
||||
}
|
||||
|
||||
// 从文件导入 PATH
|
||||
@@ -204,102 +239,97 @@ ErrorCode import_paths_from_file(const char *filepath, ExportData *data)
|
||||
int in_user = 0;
|
||||
int depth = 0;
|
||||
int in_string = 0;
|
||||
char path_buffer[4096];
|
||||
int path_len = 0;
|
||||
char key_buffer[256] = {0};
|
||||
int key_len = 0;
|
||||
|
||||
while (fgets(buffer, sizeof(buffer), fp))
|
||||
{
|
||||
char *p = buffer;
|
||||
while (*p)
|
||||
{
|
||||
if (*p == '"' && (p == buffer || *(p - 1) != '\\'))
|
||||
// 处理字符串开始/结束
|
||||
if (*p == '"')
|
||||
{
|
||||
in_string = !in_string;
|
||||
}
|
||||
else if (in_string && *p == '\\')
|
||||
{
|
||||
p++;
|
||||
if (*p)
|
||||
if (!in_string)
|
||||
{
|
||||
if (*p == 'n')
|
||||
*p = '\n';
|
||||
else if (*p == 'r')
|
||||
*p = '\r';
|
||||
else if (*p == 't')
|
||||
*p = '\t';
|
||||
// 字符串开始
|
||||
in_string = 1;
|
||||
key_len = 0; // 开始收集键名或字符串内容
|
||||
}
|
||||
else if (!is_quote_escaped(p, buffer))
|
||||
{
|
||||
// 字符串结束(未转义的引号)
|
||||
in_string = 0;
|
||||
|
||||
// 在 depth 1 时,检查刚结束的字符串是否是键名
|
||||
if (depth == 1)
|
||||
{
|
||||
key_buffer[key_len] = '\0';
|
||||
if (strcmp(key_buffer, "system") == 0)
|
||||
{
|
||||
in_system = 1;
|
||||
in_user = 0;
|
||||
}
|
||||
else if (strcmp(key_buffer, "user") == 0)
|
||||
{
|
||||
in_user = 1;
|
||||
in_system = 0;
|
||||
}
|
||||
}
|
||||
// 在 depth 2 时,如果在 system/user 数组内,提取路径
|
||||
else if (depth == 2 && (in_system || in_user))
|
||||
{
|
||||
key_buffer[key_len] = '\0';
|
||||
if (key_len > 0)
|
||||
{
|
||||
StringList *target = in_system ? &data->system : &data->user;
|
||||
add_string_list(target, key_buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 转义的引号,作为内容的一部分
|
||||
if (key_len < (int)sizeof(key_buffer) - 1)
|
||||
key_buffer[key_len++] = *p;
|
||||
}
|
||||
}
|
||||
else if (!in_string)
|
||||
else if (in_string)
|
||||
{
|
||||
// 在字符串内,收集内容
|
||||
if (*p == '\\' && *(p + 1))
|
||||
{
|
||||
// 处理转义序列
|
||||
p++;
|
||||
char ch;
|
||||
switch (*p)
|
||||
{
|
||||
case 'n': ch = '\n'; break;
|
||||
case 'r': ch = '\r'; break;
|
||||
case 't': ch = '\t'; break;
|
||||
case 'b': ch = '\b'; break;
|
||||
case 'f': ch = '\f'; break;
|
||||
case '\\': ch = '\\'; break;
|
||||
case '"': ch = '"'; break;
|
||||
case '/': ch = '/'; break;
|
||||
default: ch = *p; break;
|
||||
}
|
||||
if (key_len < (int)sizeof(key_buffer) - 1)
|
||||
key_buffer[key_len++] = ch;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (key_len < (int)sizeof(key_buffer) - 1)
|
||||
key_buffer[key_len++] = *p;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 不在字符串内
|
||||
if (*p == '{' || *p == '[')
|
||||
depth++;
|
||||
else if (*p == '}' || *p == ']')
|
||||
depth--;
|
||||
else if (depth == 1 && *p == '"')
|
||||
{
|
||||
if (strncmp(p, "\"system\"", 8) == 0)
|
||||
{
|
||||
in_system = 1;
|
||||
in_user = 0;
|
||||
}
|
||||
else if (strncmp(p, "\"user\"", 6) == 0)
|
||||
{
|
||||
in_user = 1;
|
||||
in_system = 0;
|
||||
}
|
||||
}
|
||||
else if (in_system && depth == 2 && *p == '"')
|
||||
{
|
||||
path_len = 0;
|
||||
p++;
|
||||
while (*p && path_len < (int)sizeof(path_buffer) - 1)
|
||||
{
|
||||
if (*p == '"' && *(p - 1) != '\\')
|
||||
break;
|
||||
if (*p == '\\' && *(p + 1))
|
||||
{
|
||||
p++;
|
||||
if (*p == 'n')
|
||||
*p = '\n';
|
||||
else if (*p == 'r')
|
||||
*p = '\r';
|
||||
else if (*p == 't')
|
||||
*p = '\t';
|
||||
}
|
||||
path_buffer[path_len++] = *p++;
|
||||
}
|
||||
if (path_len > 0)
|
||||
{
|
||||
path_buffer[path_len] = '\0';
|
||||
add_string_list(&data->system, path_buffer);
|
||||
}
|
||||
}
|
||||
else if (in_user && depth == 2 && *p == '"')
|
||||
{
|
||||
path_len = 0;
|
||||
p++;
|
||||
while (*p && path_len < (int)sizeof(path_buffer) - 1)
|
||||
{
|
||||
if (*p == '"' && *(p - 1) != '\\')
|
||||
break;
|
||||
if (*p == '\\' && *(p + 1))
|
||||
{
|
||||
p++;
|
||||
if (*p == 'n')
|
||||
*p = '\n';
|
||||
else if (*p == 'r')
|
||||
*p = '\r';
|
||||
else if (*p == 't')
|
||||
*p = '\t';
|
||||
}
|
||||
path_buffer[path_len++] = *p++;
|
||||
}
|
||||
if (path_len > 0)
|
||||
{
|
||||
path_buffer[path_len] = '\0';
|
||||
add_string_list(&data->user, path_buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
p++;
|
||||
}
|
||||
|
||||
+46
-22
@@ -52,44 +52,68 @@ ErrorCode path_manager_move_down(StringList *list, int index)
|
||||
}
|
||||
|
||||
// 清理无效路径项
|
||||
// 算法:先标记需要删除的项,然后从后向前批量删除,减少内存移动
|
||||
ErrorCode path_manager_clean(StringList *list)
|
||||
{
|
||||
if (!list) return ERR_NULL_PTR;
|
||||
|
||||
if (list->count == 0) return ERR_OK;
|
||||
|
||||
// 分配标记数组
|
||||
char *marks = (char *)calloc(list->count, sizeof(char));
|
||||
if (!marks) return ERR_OUT_OF_MEMORY;
|
||||
|
||||
int removed_count = 0;
|
||||
|
||||
// 第一遍:标记无效路径和重复路径
|
||||
for (int i = list->count - 1; i >= 0; i--)
|
||||
{
|
||||
char *item = list->items[i];
|
||||
if (!item) continue;
|
||||
|
||||
int should_remove = 0;
|
||||
if (!item)
|
||||
{
|
||||
marks[i] = 1;
|
||||
removed_count++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 检查路径有效性
|
||||
if (!is_path_valid(item))
|
||||
{
|
||||
should_remove = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
char *prev_item = list->items[j];
|
||||
if (prev_item && _stricmp(item, prev_item) == 0)
|
||||
{
|
||||
should_remove = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
marks[i] = 1;
|
||||
removed_count++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (should_remove)
|
||||
// 检查是否与前面的项重复(只检查未被标记的项)
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
path_manager_remove_at(list, i);
|
||||
removed_count++;
|
||||
if (!marks[j] && list->items[j] && _stricmp(item, list->items[j]) == 0)
|
||||
{
|
||||
marks[i] = 1;
|
||||
removed_count++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log_info("Cleaned paths: removed %d invalid/duplicate paths, remaining %d",
|
||||
|
||||
// 第二遍:从后向前删除标记的项,避免多次内存移动
|
||||
for (int i = list->count - 1; i >= 0; i--)
|
||||
{
|
||||
if (marks[i])
|
||||
{
|
||||
free(list->items[i]);
|
||||
// 移动后续元素
|
||||
for (int j = i; j < list->count - 1; j++)
|
||||
{
|
||||
list->items[j] = list->items[j + 1];
|
||||
}
|
||||
list->items[list->count - 1] = NULL;
|
||||
list->count--;
|
||||
}
|
||||
}
|
||||
|
||||
free(marks);
|
||||
|
||||
log_info("Cleaned paths: removed %d invalid/duplicate paths, remaining %d",
|
||||
removed_count, list->count);
|
||||
return ERR_OK;
|
||||
}
|
||||
+32
-30
@@ -26,38 +26,40 @@ static ErrorCode load_single_path(HKEY hKeyRoot, const wchar_t *regPath, StringL
|
||||
if (res == ERROR_SUCCESS)
|
||||
{
|
||||
wchar_t *buffer = (wchar_t *)malloc(size + 2);
|
||||
if (buffer)
|
||||
if (!buffer)
|
||||
{
|
||||
memset(buffer, 0, size + 2);
|
||||
if (RegQueryValueExW(hKey, REG_VALUE, NULL, &type, (LPBYTE)buffer, &size) == ERROR_SUCCESS)
|
||||
{
|
||||
wchar_t *current = buffer;
|
||||
wchar_t *next_semicolon = NULL;
|
||||
|
||||
while (*current)
|
||||
{
|
||||
next_semicolon = wcschr(current, L';');
|
||||
if (next_semicolon)
|
||||
*next_semicolon = L'\0';
|
||||
|
||||
if (wcslen(current) > 0)
|
||||
{
|
||||
char *utf8_str = wide_to_utf8(current);
|
||||
if (utf8_str)
|
||||
{
|
||||
add_string_list(list, utf8_str);
|
||||
free(utf8_str);
|
||||
}
|
||||
}
|
||||
|
||||
if (next_semicolon)
|
||||
current = next_semicolon + 1;
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(buffer);
|
||||
RegCloseKey(hKey);
|
||||
return ERR_OUT_OF_MEMORY;
|
||||
}
|
||||
memset(buffer, 0, size + 2);
|
||||
if (RegQueryValueExW(hKey, REG_VALUE, NULL, &type, (LPBYTE)buffer, &size) == ERROR_SUCCESS)
|
||||
{
|
||||
wchar_t *current = buffer;
|
||||
wchar_t *next_semicolon = NULL;
|
||||
|
||||
while (*current)
|
||||
{
|
||||
next_semicolon = wcschr(current, L';');
|
||||
if (next_semicolon)
|
||||
*next_semicolon = L'\0';
|
||||
|
||||
if (wcslen(current) > 0)
|
||||
{
|
||||
char *utf8_str = wide_to_utf8(current);
|
||||
if (utf8_str)
|
||||
{
|
||||
add_string_list(list, utf8_str);
|
||||
free(utf8_str);
|
||||
}
|
||||
}
|
||||
|
||||
if (next_semicolon)
|
||||
current = next_semicolon + 1;
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(buffer);
|
||||
}
|
||||
RegCloseKey(hKey);
|
||||
return ERR_OK;
|
||||
|
||||
Reference in New Issue
Block a user