VScript: properly handle internal errors during return

Must always check for internal error before returning out of a script function. The macro SCRIPT_CHECK_AND_RETURN will deal with this. Replaced all returns in each script func.
This commit is contained in:
Kawe Mazidjatari 2024-03-31 17:01:32 +02:00
parent f86fc0d405
commit cf93cb3101
5 changed files with 59 additions and 85 deletions

View File

@ -89,7 +89,7 @@ namespace VScriptCode
g_ServerListManager.RefreshServerList(serverMessage, iCount);
sq_pushinteger(v, static_cast<SQInteger>(iCount));
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -100,7 +100,7 @@ namespace VScriptCode
size_t iCount = g_ServerListManager.m_vServerList.size();
sq_pushinteger(v, static_cast<SQInteger>(iCount));
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -111,7 +111,7 @@ namespace VScriptCode
SQChar* privateToken = sq_getstring(v, 1);
if (!VALID_CHARSTAR(privateToken))
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
string hiddenServerRequestMessage;
NetGameServer_t serverListing;
@ -120,28 +120,22 @@ namespace VScriptCode
if (!result)
{
if (hiddenServerRequestMessage.empty())
{
sq_pushstring(v, "Request failed", -1);
}
else
{
hiddenServerRequestMessage = Format("Request failed: %s", hiddenServerRequestMessage.c_str());
sq_pushstring(v, hiddenServerRequestMessage.c_str(), -1);
}
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
if (serverListing.name.empty())
{
if (hiddenServerRequestMessage.empty())
{
hiddenServerRequestMessage = Format("Server listing empty");
}
else
{
hiddenServerRequestMessage = Format("Server listing empty: %s", hiddenServerRequestMessage.c_str());
}
sq_pushstring(v, hiddenServerRequestMessage.c_str(), -1);
}
@ -151,7 +145,7 @@ namespace VScriptCode
sq_pushstring(v, hiddenServerRequestMessage.c_str(), -1);
}
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -163,14 +157,12 @@ namespace VScriptCode
SQInteger iServer = sq_getinteger(v, 1);
if (!Script_CheckServerIndex(v, iServer))
{
return SQ_ERROR;
}
SCRIPT_CHECK_AND_RETURN(v, SQ_ERROR);
const string& serverName = g_ServerListManager.m_vServerList[iServer].name;
sq_pushstring(v, serverName.c_str(), -1);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -182,14 +174,12 @@ namespace VScriptCode
SQInteger iServer = sq_getinteger(v, 1);
if (!Script_CheckServerIndex(v, iServer))
{
return SQ_ERROR;
}
SCRIPT_CHECK_AND_RETURN(v, SQ_ERROR);
const string& serverDescription = g_ServerListManager.m_vServerList[iServer].description;
sq_pushstring(v, serverDescription.c_str(), -1);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -201,14 +191,12 @@ namespace VScriptCode
SQInteger iServer = sq_getinteger(v, 1);
if (!Script_CheckServerIndex(v, iServer))
{
return SQ_ERROR;
}
SCRIPT_CHECK_AND_RETURN(v, SQ_ERROR);
const string& svServerMapName = g_ServerListManager.m_vServerList[iServer].map;
sq_pushstring(v, svServerMapName.c_str(), -1);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -220,14 +208,12 @@ namespace VScriptCode
SQInteger iServer = sq_getinteger(v, 1);
if (!Script_CheckServerIndex(v, iServer))
{
return SQ_ERROR;
}
SCRIPT_CHECK_AND_RETURN(v, SQ_ERROR);
const string& serverPlaylist = g_ServerListManager.m_vServerList[iServer].playlist;
sq_pushstring(v, serverPlaylist.c_str(), -1);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -239,14 +225,12 @@ namespace VScriptCode
SQInteger iServer = sq_getinteger(v, 1);
if (!Script_CheckServerIndex(v, iServer))
{
return SQ_ERROR;
}
SCRIPT_CHECK_AND_RETURN(v, SQ_ERROR);
const SQInteger playerCount = g_ServerListManager.m_vServerList[iServer].numPlayers;
sq_pushinteger(v, playerCount);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -258,14 +242,12 @@ namespace VScriptCode
SQInteger iServer = sq_getinteger(v, 1);
if (!Script_CheckServerIndex(v, iServer))
{
return SQ_ERROR;
}
SCRIPT_CHECK_AND_RETURN(v, SQ_ERROR);
const SQInteger maxPlayers = g_ServerListManager.m_vServerList[iServer].maxPlayers;
sq_pushinteger(v, maxPlayers);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -326,7 +308,7 @@ namespace VScriptCode
}
sq_pushstring(v, pszPromoKey, -1);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
SQRESULT GetEULAContents(HSQUIRRELVM v)
@ -349,7 +331,7 @@ namespace VScriptCode
sq_pushstring(v, error.c_str(), -1);
}
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -361,12 +343,12 @@ namespace VScriptCode
SQChar* cryptoKey = sq_getstring(v, 2);
if (!VALID_CHARSTAR(ipAddress) || VALID_CHARSTAR(cryptoKey))
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
Msg(eDLL_T::UI, "Connecting to server with ip address '%s' and encryption key '%s'\n", ipAddress, cryptoKey);
g_ServerListManager.ConnectToServer(ipAddress, cryptoKey);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -379,7 +361,7 @@ namespace VScriptCode
if (!Script_CheckServerIndex(v, iServer))
{
return SQ_ERROR;
SCRIPT_CHECK_AND_RETURN(v, SQ_ERROR);
}
const NetGameServer_t& gameServer = g_ServerListManager.m_vServerList[iServer];
@ -387,7 +369,7 @@ namespace VScriptCode
g_ServerListManager.ConnectToServer(gameServer.address, gameServer.port,
gameServer.netKey);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -398,7 +380,7 @@ namespace VScriptCode
SQChar* privateToken = sq_getstring(v, 1);
if (!VALID_CHARSTAR(privateToken))
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
string hiddenServerRequestMessage;
NetGameServer_t netListing;
@ -413,7 +395,7 @@ namespace VScriptCode
Warning(eDLL_T::UI, "Failed to connect to private server: %s\n", hiddenServerRequestMessage.c_str());
}
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -422,7 +404,7 @@ namespace VScriptCode
SQRESULT IsClientDLL(HSQUIRRELVM v)
{
sq_pushbool(v, ::IsClientDLL());
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
}
}

View File

@ -1990,15 +1990,13 @@ namespace VScriptCode
SQRESULT VScriptCode::Server::LiveAPI_IsValidToRun(HSQUIRRELVM v)
{
sq_pushbool(v, liveapi_enabled.GetBool());
SCRIPT_CHECK_INTERNAL_ERROR(v);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
SQRESULT VScriptCode::Server::LiveAPI_LogRaw(HSQUIRRELVM v)
{
if (!LiveAPISystem()->IsEnabled())
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
SQRESULT result = SQ_OK;
SQObjectPtr& object = v->GetUp(-2);
@ -2017,8 +2015,7 @@ SQRESULT VScriptCode::Server::LiveAPI_LogRaw(HSQUIRRELVM v)
result = SQ_FAIL;
}
SCRIPT_CHECK_INTERNAL_ERROR(v);
return result;
SCRIPT_CHECK_AND_RETURN(v, result);
}
void Script_RegisterLiveAPIFunctions(CSquirrelVM* const s)

View File

@ -58,7 +58,7 @@ namespace VScriptCode
!VALID_CHARSTAR(serverMapName) ||
!VALID_CHARSTAR(serverPlaylist))
{
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
// Adjust browser settings.
@ -73,10 +73,7 @@ namespace VScriptCode
g_ServerHostManager.SetVisibility(serverVisibility);
g_ServerHostManager.LaunchServer(g_pServer->IsActive());
return SQ_OK;
//v_SQVM_RaiseError(v, "\"%s\" is not supported on client builds.\n", "CreateServer");
//return SQ_ERROR;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
// Purpose: shuts the server down and disconnects all clients
@ -86,7 +83,7 @@ namespace VScriptCode
if (g_pHostState->m_bActiveGame)
g_pHostState->m_iNextState = HostStates_t::HS_GAME_SHUTDOWN;
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -102,8 +99,7 @@ namespace VScriptCode
reason = nullptr;
g_BanSystem.KickPlayerByName(playerName, reason);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -119,8 +115,7 @@ namespace VScriptCode
reason = nullptr;
g_BanSystem.KickPlayerById(playerHandle, reason);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -136,8 +131,7 @@ namespace VScriptCode
reason = nullptr;
g_BanSystem.BanPlayerByName(playerName, reason);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -153,8 +147,7 @@ namespace VScriptCode
reason = nullptr;
g_BanSystem.BanPlayerById(playerHandle, reason);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -165,7 +158,7 @@ namespace VScriptCode
SQChar* szCriteria = sq_getstring(v, 1);
g_BanSystem.UnbanPlayer(szCriteria);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -174,7 +167,7 @@ namespace VScriptCode
SQRESULT GetNumHumanPlayers(HSQUIRRELVM v)
{
sq_pushinteger(v, g_pServer->GetNumHumanPlayers());
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -183,7 +176,7 @@ namespace VScriptCode
SQRESULT GetNumFakeClients(HSQUIRRELVM v)
{
sq_pushinteger(v, g_pServer->GetNumFakeClients());
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -192,9 +185,9 @@ namespace VScriptCode
SQRESULT IsServerActive(HSQUIRRELVM v)
{
bool isActive = g_pServer->IsActive();
sq_pushbool(v, isActive);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -203,7 +196,7 @@ namespace VScriptCode
SQRESULT IsDedicated(HSQUIRRELVM v)
{
sq_pushbool(v, ::IsDedicated());
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
}
}

View File

@ -28,7 +28,7 @@ namespace VScriptCode
SQRESULT GetSDKVersion(HSQUIRRELVM v)
{
sq_pushstring(v, SDK_VERSION, -1);
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -39,7 +39,7 @@ namespace VScriptCode
std::lock_guard<std::mutex> l(g_InstalledMapsMutex);
if (g_InstalledMaps.IsEmpty())
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
sq_newarray(v, 0);
@ -51,7 +51,7 @@ namespace VScriptCode
sq_arrayappend(v, -2);
}
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
//-----------------------------------------------------------------------------
@ -62,7 +62,7 @@ namespace VScriptCode
std::lock_guard<std::mutex> l(g_PlaylistsVecMutex);
if (g_vAllPlaylists.empty())
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
sq_newarray(v, 0);
for (const string& it : g_vAllPlaylists)
@ -71,7 +71,7 @@ namespace VScriptCode
sq_arrayappend(v, -2);
}
return SQ_OK;
SCRIPT_CHECK_AND_RETURN(v, SQ_OK);
}
SQRESULT ScriptError(HSQUIRRELVM v)
@ -80,12 +80,10 @@ namespace VScriptCode
SQInteger a4 = 0;
if (SQVM_sprintf(v, 0, 1, &a4, &pString) < 0)
return SQ_ERROR;
SCRIPT_CHECK_AND_RETURN(v, SQ_ERROR);
v_SQVM_ScriptError("%s", pString);
SCRIPT_CHECK_INTERNAL_ERROR(v);
return SQ_ERROR;
SCRIPT_CHECK_AND_RETURN(v, SQ_ERROR);
}
}
}

View File

@ -94,12 +94,16 @@ inline CSquirrelVM* g_pUIScript;
Error(context, EXIT_FAILURE, "Error adding enum '%s' to const table.", enumName); \
sq_endconsttable(v); \
// Place this at the end of every script func
#define SCRIPT_CHECK_INTERNAL_ERROR(v) \
SQSharedState* const sharedState = v->_sharedstate; \
if (sharedState->_internal_error) { \
CSquirrelVM__ThrowError(sharedState->_scriptvm, v); \
return SQ_ERROR; \
// Use this to return from any script func
#define SCRIPT_CHECK_AND_RETURN(v, val) \
{ \
SQSharedState* const sharedState = v->_sharedstate; \
if (sharedState->_internal_error) { \
\
CSquirrelVM__ThrowError(sharedState->_scriptvm, v); \
return SQ_ERROR; \
} \
return val; \
}
///////////////////////////////////////////////////////////////////////////////