refactor: 移除未使用的布局配置并增强日志与安全字符串

- 删除未使用的 layout_config.h 头文件及其引用
- 在 main.c 和 callbacks.c 中添加日志记录以追踪程序启动、关闭和关键操作
- 将多处 strncpy 调用替换为安全的 safe_strcpy 函数
- 在 dialogs.c 中引入 safe_string.h 以使用安全字符串函数
This commit is contained in:
2026-03-26 13:20:34 +08:00
parent 6ba7e702f2
commit 3af0e96060
5 changed files with 28 additions and 27 deletions
-1
View File
@@ -53,7 +53,6 @@
* 统一错误码系统 (`utils/error_code.h`) * 统一错误码系统 (`utils/error_code.h`)
* 安全字符串函数 (`utils/safe_string.h`) * 安全字符串函数 (`utils/safe_string.h`)
* 日志系统 (`utils/logger.h`) * 日志系统 (`utils/logger.h`)
* 布局配置结构体 (`core/layout_config.h`)
## 📦 下载与安装 ## 📦 下载与安装
-19
View File
@@ -1,19 +0,0 @@
#ifndef LAYOUT_CONFIG_H
#define LAYOUT_CONFIG_H
// 布局配置结构体
typedef struct {
int vbox_gap; // 垂直布局间距
int vbox_margin_width; // 垂直布局外边距宽度
int vbox_margin_height; // 垂直布局外边距高度
int hbox_gap; // 水平布局间距
int hbox_margin_width; // 水平布局外边距宽度
int hbox_margin_height; // 水平布局外边距高度
int button_width; // 按钮宽度
int button_height; // 按钮高度
} LayoutConfig;
// 默认布局配置
extern const LayoutConfig DEFAULT_LAYOUT;
#endif // LAYOUT_CONFIG_H
+17 -5
View File
@@ -7,6 +7,8 @@
#include "utils/string_ext.h" #include "utils/string_ext.h"
#include "utils/os_env.h" #include "utils/os_env.h"
#include "utils/error_code.h" #include "utils/error_code.h"
#include "utils/safe_string.h"
#include "utils/logger.h"
#include "ui/ui_utils.h" #include "ui/ui_utils.h"
#include "ui/dialogs.h" #include "ui/dialogs.h"
#include <string.h> #include <string.h>
@@ -86,8 +88,7 @@ int btn_edit_cb(Ihandle *self)
return IUP_DEFAULT; return IUP_DEFAULT;
char buffer[4096]; char buffer[4096];
strncpy(buffer, raw_data->items[selected - 1], 4096); safe_strcpy(buffer, sizeof(buffer), raw_data->items[selected - 1]);
buffer[4095] = '\0';
if (custom_input_dialog("编辑环境变量", "编辑路径:", buffer, sizeof(buffer))) if (custom_input_dialog("编辑环境变量", "编辑路径:", buffer, sizeof(buffer)))
{ {
@@ -331,6 +332,7 @@ int btn_ok_cb(Ihandle *self)
if (sys_ok == ERR_OK && user_ok == ERR_OK) if (sys_ok == ERR_OK && user_ok == ERR_OK)
{ {
log_info("Saved system paths: %d, user paths: %d", ctx->sys_paths.count, ctx->user_paths.count);
SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)L"Environment", SMTO_ABORTIFHUNG, 5000, NULL); SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)L"Environment", SMTO_ABORTIFHUNG, 5000, NULL);
IupMessage("成功", "系统和用户 PATH 环境变量均已更新!"); IupMessage("成功", "系统和用户 PATH 环境变量均已更新!");
if (lbl_status) if (lbl_status)
@@ -346,6 +348,7 @@ int btn_ok_cb(Ihandle *self)
} }
else else
{ {
log_error("Failed to save paths: sys=%d, user=%d", sys_ok, user_ok);
IupMessage("错误", "保存失败!"); IupMessage("错误", "保存失败!");
if (lbl_status) if (lbl_status)
IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "error")); IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "error"));
@@ -495,8 +498,7 @@ int btn_export_cb(Ihandle *self)
} }
else else
{ {
strncpy(final_path, filepath, sizeof(final_path) - 1); safe_strcpy(final_path, sizeof(final_path), filepath);
final_path[sizeof(final_path) - 1] = '\0';
} }
filepath = final_path; filepath = final_path;
@@ -529,9 +531,19 @@ void load_all_paths(void)
if (load_system_paths(&ctx->sys_paths) != ERR_OK) if (load_system_paths(&ctx->sys_paths) != ERR_OK)
{ {
log_error("Failed to load system paths");
IupMessage("错误", "无法打开系统环境变量注册表键,请尝试以管理员身份运行。"); IupMessage("错误", "无法打开系统环境变量注册表键,请尝试以管理员身份运行。");
} }
load_user_paths(&ctx->user_paths); else
{
log_info("Loaded system paths: %d", ctx->sys_paths.count);
}
ErrorCode user_result = load_user_paths(&ctx->user_paths);
if (user_result == ERR_OK)
{
log_info("Loaded user paths: %d", ctx->user_paths.count);
}
Ihandle *list_sys = IupGetDialogChild(dlg, "LIST_SYS"); Ihandle *list_sys = IupGetDialogChild(dlg, "LIST_SYS");
Ihandle *list_user = IupGetDialogChild(dlg, "LIST_USER"); Ihandle *list_user = IupGetDialogChild(dlg, "LIST_USER");
+9
View File
@@ -7,6 +7,7 @@
#include "core/lua_config.h" #include "core/lua_config.h"
#include "utils/string_ext.h" #include "utils/string_ext.h"
#include "utils/os_env.h" #include "utils/os_env.h"
#include "utils/logger.h"
#include "controller/callbacks.h" #include "controller/callbacks.h"
#include "ui/main_window.h" #include "ui/main_window.h"
@@ -33,6 +34,10 @@ build\\PathEditor.exe
// 主函数 // 主函数
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
// 初始化日志系统
log_init(NULL, LOG_LEVEL_INFO);
log_info("PathEditor starting...");
// 强制设置 UTF8MODE 环境变量,必须在 IupOpen 之前 // 强制设置 UTF8MODE 环境变量,必须在 IupOpen 之前
putenv("IUP_UTF8MODE=YES"); putenv("IUP_UTF8MODE=YES");
@@ -44,6 +49,8 @@ int main(int argc, char **argv)
IupMessage("警告", "Lua 配置系统初始化失败,将使用默认值"); IupMessage("警告", "Lua 配置系统初始化失败,将使用默认值");
} }
log_info("Lua config initialized");
// 在管理员模式下,解决无法拖拽文件到列表框的问题 (UIPI) // 在管理员模式下,解决无法拖拽文件到列表框的问题 (UIPI)
// 需要加载 User32.dll 获取 ChangeWindowMessageFilter 函数 // 需要加载 User32.dll 获取 ChangeWindowMessageFilter 函数
HMODULE hUser32 = LoadLibraryW(L"user32.dll"); HMODULE hUser32 = LoadLibraryW(L"user32.dll");
@@ -132,8 +139,10 @@ int main(int argc, char **argv)
IupMainLoop(); IupMainLoop();
log_info("PathEditor exiting...");
destroy_app_context(ctx); destroy_app_context(ctx);
lua_config_destroy(); lua_config_destroy();
log_destroy();
IupClose(); IupClose();
return 0; return 0;
+2 -2
View File
@@ -1,5 +1,6 @@
#include "ui/dialogs.h" #include "ui/dialogs.h"
#include "core/lua_config.h" #include "core/lua_config.h"
#include "utils/safe_string.h"
#include <iup.h> #include <iup.h>
#include <string.h> #include <string.h>
@@ -61,8 +62,7 @@ int custom_input_dialog(const char *title, const char *label_text, char *buffer,
char *val = IupGetAttribute(text, "VALUE"); char *val = IupGetAttribute(text, "VALUE");
if (val) if (val)
{ {
strncpy(buffer, val, buffer_size); safe_strcpy(buffer, buffer_size, val);
buffer[buffer_size - 1] = '\0';
} }
} }