fix: 修复JSON导入、备份目录创建和内存安全等问题

修复JSON导入时转义字符处理不完整的问题,添加对\b、\f等控制字符的转义
改进备份目录创建逻辑,使用SHCreateDirectoryExW递归创建目录
修复内存分配失败处理,避免空指针解引用
修正选项卡标题设置位置,从Dialog改为Tabs控件
增强导入功能,支持TXT文件导入时选择目标变量类型
优化清理无效路径算法,使用标记数组减少内存移动
修复宽字符环境变量设置,使用_wputenv_s替代putenv
添加导入数据初始化,防止未初始化内存访问
改进文件属性检查,使用宽字符API支持Unicode路径
This commit is contained in:
2026-04-28 22:21:06 +08:00
parent 7908bad1f4
commit e777b26879
11 changed files with 257 additions and 157 deletions
+12 -1
View File
@@ -42,6 +42,8 @@ int btn_import_cb(Ihandle *self)
if (filepath)
{
ExportData imported;
init_string_list(&imported.system);
init_string_list(&imported.user);
ErrorCode import_result = import_paths_from_file(filepath, &imported);
if (import_result == ERR_OK)
{
@@ -51,6 +53,8 @@ int btn_import_cb(Ihandle *self)
if (!has_system && !has_user)
{
IupMessage("错误", "文件中没有找到有效的路径!");
clear_string_list(&imported.system);
clear_string_list(&imported.user);
IupDestroy(filedlg);
return IUP_DEFAULT;
}
@@ -63,7 +67,10 @@ int btn_import_cb(Ihandle *self)
}
else if (has_system)
{
choice = 3;
// TXT 文件导入时,让用户选择目标(系统变量或用户变量)
choice = IupAlarm("导入选项", "请选择导入目标:",
"导入到系统变量", "导入到用户变量", NULL);
// IupAlarm 返回 1 或 2,转换为 1(系统) 或 2(用户)
}
else
{
@@ -96,6 +103,10 @@ int btn_import_cb(Ihandle *self)
total_imported += imported.user.count;
}
// 释放导入数据
clear_string_list(&imported.system);
clear_string_list(&imported.user);
char msg[256];
snprintf(msg, sizeof(msg), "成功导入 %d 个路径!", total_imported);
IupMessage("导入成功", msg);
+4 -1
View File
@@ -3,6 +3,7 @@
#include "core/app_context.h"
#include "core/lua_config.h"
#include "utils/string_ext.h"
#include "utils/safe_string.h"
#include "utils/ui_constants.h"
#include "ui/ui_utils.h"
#include <string.h>
@@ -55,7 +56,9 @@ int list_dropfiles_cb(Ihandle *self, const char *filename, int num, int x, int y
else
return IUP_DEFAULT;
DWORD attr = GetFileAttributesA(filename);
wchar_t *wfilename = utf8_to_wide(filename);
DWORD attr = wfilename ? GetFileAttributesW(wfilename) : INVALID_FILE_ATTRIBUTES;
free(wfilename);
if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY))
{
Ihandle *txt_search = IupGetDialogChild(dlg, CTRL_TXT_SEARCH);
+9 -1
View File
@@ -28,7 +28,15 @@ int btn_ok_cb(Ihandle *self)
return IUP_DEFAULT;
}
backup_registry();
ErrorCode backup_result = backup_registry();
if (backup_result != ERR_OK)
{
log_error("Backup failed: error code %d", backup_result);
int choice = IupAlarm("警告", "备份失败!是否继续保存?\n(继续保存可能导致无法恢复)",
"继续保存", "取消", NULL);
if (choice != 1)
return IUP_DEFAULT;
}
ErrorCode sys_ok = save_system_paths(&ctx->sys_paths);
ErrorCode user_ok = save_user_paths(&ctx->user_paths);