From d80d098b64d23d2719170958867fbf8a76008009 Mon Sep 17 00:00:00 2001 From: qyt <486179@qq.com> Date: Sat, 9 May 2026 06:00:44 +0000 Subject: [PATCH] Potential fix for code scanning alert no. 59 Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --- .../ModuleConfigure_Json/ModuleConfigure_Json.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/XEngine_Source/XEngine_ModuleConfigure/ModuleConfigure_Json/ModuleConfigure_Json.cpp b/XEngine_Source/XEngine_ModuleConfigure/ModuleConfigure_Json/ModuleConfigure_Json.cpp index 70bd524..ec2a074 100644 --- a/XEngine_Source/XEngine_ModuleConfigure/ModuleConfigure_Json/ModuleConfigure_Json.cpp +++ b/XEngine_Source/XEngine_ModuleConfigure/ModuleConfigure_Json/ModuleConfigure_Json.cpp @@ -42,18 +42,23 @@ CModuleConfigure_Json::~CModuleConfigure_Json() *********************************************************************/ bool CModuleConfigure_Json::ModuleConfigure_Json_File(LPCXSTR lpszConfigFile, XENGINE_SERVICECONFIG* pSt_ServerConfig) { + // 约定:每次进入函数先清除错误状态,后续任一步失败都设置错误码并返回 false。 Config_IsErrorOccur = false; + // 第一步:校验输入参数,配置文件路径和输出结构体都不能为空。 if ((NULL == lpszConfigFile) || (NULL == pSt_ServerConfig)) { Config_IsErrorOccur = true; Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_PARAMENT; return false; } + + // 第二步:准备 JSON 解析器对象与错误信息缓冲。 Json::Value st_JsonRoot; JSONCPP_STRING st_JsonError; Json::CharReaderBuilder st_JsonBuilder; - //读取配置文件所有内容到缓冲区 + + // 第三步:读取配置文件内容到固定缓冲区(当前实现最多读取 8192 字节)。 FILE* pSt_File = _xtfopen(lpszConfigFile, _X("rb")); if (NULL == pSt_File) { @@ -64,7 +69,8 @@ bool CModuleConfigure_Json::ModuleConfigure_Json_File(LPCXSTR lpszConfigFile, XE XCHAR tszMsgBuffer[8192]; int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_File); fclose(pSt_File); - //开始解析配置文件 + + // 第四步:解析 JSON 文本。解析失败时返回统一的解析错误码。 std::unique_ptr const pSt_JsonReader(st_JsonBuilder.newCharReader()); if (!pSt_JsonReader->parse(tszMsgBuffer, tszMsgBuffer + nRet, &st_JsonRoot, &st_JsonError)) { @@ -72,6 +78,8 @@ bool CModuleConfigure_Json::ModuleConfigure_Json_File(LPCXSTR lpszConfigFile, XE Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_PARSE; return false; } + + // 第五步:将 JSON 字段映射到服务配置结构体,供调用方后续使用。 _tcsxcpy(pSt_ServerConfig->tszIPAddr, st_JsonRoot["tszIPAddr"].asCString()); pSt_ServerConfig->bDeamon = st_JsonRoot["bDeamon"].asBool(); pSt_ServerConfig->nHttpPort = st_JsonRoot["nHttpPort"].asInt();