Skip to content

Commit f9b07ce

Browse files
committed
Improve value checking on native code
1 parent 7c99533 commit f9b07ce

File tree

5 files changed

+97
-271
lines changed

5 files changed

+97
-271
lines changed

source/main.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <string>
77
#include <GarrysMod/FactoryLoader.hpp>
88
#include <Platform.hpp>
9+
#include <limits>
910

1011
class IVEngineServer;
1112
class IVEngineClient;
@@ -42,4 +43,26 @@ namespace global
4243
LUA_FUNCTION_DECLARE( GetTable );
4344

4445
void CheckType( GarrysMod::Lua::ILuaBase *LUA, int32_t index, int32_t type, const char *nametype );
46+
47+
template<typename NumericType>
48+
inline NumericType GetNumber( GarrysMod::Lua::ILuaBase *LUA, int32_t idx,
49+
NumericType min = std::numeric_limits<NumericType>::min( ),
50+
NumericType max = std::numeric_limits<NumericType>::max( ) )
51+
{
52+
double number = LUA->CheckNumber( idx );
53+
if( number < static_cast<double>( min ) )
54+
{
55+
auto extra = LUA->PushFormattedString( "number %f is less than minimum limit of %f",
56+
number, static_cast<double>( min ) );
57+
LUA->ArgError( idx, extra );
58+
}
59+
else if( number > static_cast<double>( max ) )
60+
{
61+
auto extra = LUA->PushFormattedString( "number %f is greater than maximum limit of %f",
62+
number, static_cast<double>( min ) );
63+
LUA->ArgError( idx, extra );
64+
}
65+
66+
return static_cast<NumericType>( number );
67+
}
4568
}

source/netmessage.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,12 @@ namespace NetMessage
481481
{
482482
GarrysMod::Lua::ILuaBase *LUA = L->luabase;
483483
LUA->SetState( L );
484-
Push( LUA, new( std::nothrow ) NetMessage );
484+
CNetChan *netchan = NetChannel::Get( LUA, 1 );
485+
NetMessage *msg = new( std::nothrow ) NetMessage;
486+
if( msg != nullptr )
487+
msg->SetNetChannel( netchan );
488+
489+
Push( LUA, new( std::nothrow ) NetMessage, netchan );
485490
return 1;
486491
}
487492

0 commit comments

Comments
 (0)