Skip to content

Commit 492f02d

Browse files
Potential fix for code scanning alert no. 45: Incorrect allocation-error handling
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent fb4f494 commit 492f02d

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

XEngine_Module/XEngine_AIApi/AIApi_Chat/AIApi_Chat.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "pch.h"
22
#include "AIApi_Chat.h"
3+
#include <new>
34
/********************************************************************
45
// Created: 2025/05/29 13:57:18
56
// File Name: D:\XEngine_OPenSource\XEngine_Module\XEngine_AIApi\AIApi_Chat\AIApi_Chat.cpp
@@ -68,7 +69,7 @@ bool CAIApi_Chat::AIApi_Chat_Create(XNETHANDLE* pxhToken, LPCXSTR lpszAPIUrl, LP
6869
AIApi_dwErrorCode = ERROR_XENGINE_MODULE_AIAPI_CHAT_PARAMENT;
6970
return false;
7071
}
71-
AICLIENT_CHAT *pSt_AIClient = new AICLIENT_CHAT;
72+
AICLIENT_CHAT *pSt_AIClient = new (std::nothrow) AICLIENT_CHAT;
7273
if (NULL == pSt_AIClient)
7374
{
7475
AIApi_IsErrorOccur = true;
@@ -84,18 +85,20 @@ bool CAIApi_Chat::AIApi_Chat_Create(XNETHANDLE* pxhToken, LPCXSTR lpszAPIUrl, LP
8485
_xstrcpy(pSt_AIClient->tszAPIUrl, lpszAPIUrl, sizeof(pSt_AIClient->tszAPIUrl));
8586
_xsntprintf(pSt_AIClient->tszAPIHdr, sizeof(pSt_AIClient->tszAPIHdr), _X("Content-Type: application/json\r\nAuthorization: Bearer %s"), lpszAPIKey);
8687

87-
pSt_AIClient->pStl_ListHistory = std::make_unique<std::list<AICLIENT_HISTORY>>();
88+
pSt_AIClient->pStl_ListHistory.reset(new (std::nothrow) std::list<AICLIENT_HISTORY>());
8889
if (NULL == pSt_AIClient->pStl_ListHistory)
8990
{
9091
AIApi_IsErrorOccur = true;
9192
AIApi_dwErrorCode = ERROR_XENGINE_MODULE_AIAPI_CHAT_MALLOC;
93+
delete pSt_AIClient;
9294
return false;
9395
}
9496
pSt_AIClient->ptszMSGBuffer = (XCHAR*)malloc(XENGINE_MEMORY_SIZE_MAX);
9597
if (NULL == pSt_AIClient->ptszMSGBuffer)
9698
{
9799
AIApi_IsErrorOccur = true;
98100
AIApi_dwErrorCode = ERROR_XENGINE_MODULE_AIAPI_CHAT_MALLOC;
101+
delete pSt_AIClient;
99102
return false;
100103
}
101104
memset(pSt_AIClient->ptszMSGBuffer, '\0', XENGINE_MEMORY_SIZE_MAX);
@@ -104,6 +107,8 @@ bool CAIApi_Chat::AIApi_Chat_Create(XNETHANDLE* pxhToken, LPCXSTR lpszAPIUrl, LP
104107
{
105108
AIApi_IsErrorOccur = true;
106109
AIApi_dwErrorCode = APIClient_GetLastError();
110+
free(pSt_AIClient->ptszMSGBuffer);
111+
delete pSt_AIClient;
107112
return false;
108113
}
109114
XCLIENT_APIHTTP st_HTTPParam = {};
@@ -113,12 +118,18 @@ bool CAIApi_Chat::AIApi_Chat_Create(XNETHANDLE* pxhToken, LPCXSTR lpszAPIUrl, LP
113118
{
114119
AIApi_IsErrorOccur = true;
115120
AIApi_dwErrorCode = APIClient_GetLastError();
121+
APIClient_Http_Destroy(pSt_AIClient->xhToken);
122+
free(pSt_AIClient->ptszMSGBuffer);
123+
delete pSt_AIClient;
116124
return false;
117125
}
118126
if (!APIClient_Http_SetUrl(pSt_AIClient->xhToken, lpszAPIUrl, _X("POST")))
119127
{
120128
AIApi_IsErrorOccur = true;
121129
AIApi_dwErrorCode = APIClient_GetLastError();
130+
APIClient_Http_Destroy(pSt_AIClient->xhToken);
131+
free(pSt_AIClient->ptszMSGBuffer);
132+
delete pSt_AIClient;
122133
return false;
123134
}
124135
*pxhToken = pSt_AIClient->xhToken;

0 commit comments

Comments
 (0)