From caf321bb9d10d4e3434e81d8447d2d6f333a20ee Mon Sep 17 00:00:00 2001 From: qyt <486179@qq.com> Date: Fri, 8 May 2026 10:16:13 +0000 Subject: [PATCH] Potential fix for code scanning alert no. 2 Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --- .../Session_Stroage/Session_DLStroage.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/XEngine_Source/StorageModule_Session/Session_Stroage/Session_DLStroage.cpp b/XEngine_Source/StorageModule_Session/Session_Stroage/Session_DLStroage.cpp index 957c9c5..a6323c7 100644 --- a/XEngine_Source/StorageModule_Session/Session_Stroage/Session_DLStroage.cpp +++ b/XEngine_Source/StorageModule_Session/Session_Stroage/Session_DLStroage.cpp @@ -142,9 +142,19 @@ bool CSession_DLStroage::Session_DLStroage_Insert(LPCXSTR lpszClientAddr, LPCXST struct _xtstat st_FStat; memset(&st_Client, '\0', sizeof(SESSION_STORAGEINFO)); - int nRet = _xtstat(lpszFileDir, &st_FStat); + //先打开文件,再基于已打开句柄获取文件属性,避免TOCTOU + st_Client.pSt_File = _xtfopen(lpszFileDir, _X("rb")); + if (NULL == st_Client.pSt_File) + { + Session_IsErrorOccur = true; + Session_dwErrorCode = ERROR_STORAGE_MODULE_SESSION_OPENFILE; + return false; + } + int nRet = _xtfstat(_xtfileno(st_Client.pSt_File), &st_FStat); if (-1 == nRet) { + xtfclose(st_Client.pSt_File); + st_Client.pSt_File = NULL; Session_IsErrorOccur = true; Session_dwErrorCode = ERROR_STORAGE_MODULE_SESSION_OPENFILE; return false; @@ -166,13 +176,6 @@ bool CSession_DLStroage::Session_DLStroage_Insert(LPCXSTR lpszClientAddr, LPCXST st_Client.xhToken = xhToken; } //填充下载信息 - st_Client.pSt_File = _xtfopen(lpszFileDir, _X("rb")); - if (NULL == st_Client.pSt_File) - { - Session_IsErrorOccur = true; - Session_dwErrorCode = ERROR_STORAGE_MODULE_SESSION_OPENFILE; - return false; - } //是否有范围 if ((nPosStart > 0) || (nPostEnd > 0)) {