Skip to content

Commit f0a2a86

Browse files
committed
Use errcodes instead
1 parent 573102c commit f0a2a86

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

source/socket.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,29 @@
33
extern "C" int luaopen_socket_core( lua_State *state );
44

55
int parseWhitelist(GarrysMod::Lua::ILuaBase* LUA);
6+
enum : int
7+
{
8+
PARSE_SUCCESS = 0,
9+
PARSE_CANT_READ = 1,
10+
PARSE_NO_ENTRIES = 2
11+
};
612

713
GMOD_MODULE_OPEN( )
814
{
915
#ifdef USE_WHITELIST
10-
// This might long-jump via lua error so watch out!
11-
if (parseWhitelist(LUA))
12-
{
13-
return 0;
14-
}
16+
switch (parseWhitelist(LUA))
17+
{
18+
case PARSE_SUCCESS:
19+
break;
20+
case PARSE_CANT_READ:
21+
LUA->ThrowError("Failed to read whitelist file!");
22+
break;
23+
case PARSE_NO_ENTRIES:
24+
LUA->ThrowError("Didn't find any valid entries in whitelist file!");
25+
break;
26+
default:
27+
break;
28+
}
1529
#endif
1630

1731
if( luaopen_socket_core( LUA->GetState( ) ) == 1 )

source/whitelist.cpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,36 @@
1212
const char* whitelistDir = "../gm_socket_whitelist.txt";
1313
std::vector<std::pair<std::string, std::string> > whitelist;
1414

15+
enum : int
16+
{
17+
PARSE_SUCCESS = 0,
18+
PARSE_CANT_READ = 1,
19+
PARSE_NO_ENTRIES = 2
20+
};
21+
1522
int parseWhitelist(GarrysMod::Lua::ILuaBase* LUA)
1623
{
24+
std::ifstream input(whitelistDir);
25+
if (input)
1726
{
18-
std::ifstream input(whitelistDir);
19-
if (input)
27+
std::stringstream filereader;
28+
filereader << input.rdbuf();
29+
std::string filedata = filereader.str();
30+
std::regex line_parser("([\\w\\.-]+)\\s+(\\d+)(?:(?:\r?\n)+|$)");
31+
for (std::regex_iterator<std::string::iterator> match = std::regex_iterator(filedata.begin(), filedata.end(), line_parser), end = std::regex_iterator<std::string::iterator>(); match != end; ++match)
2032
{
21-
std::stringstream filereader;
22-
filereader << input.rdbuf();
23-
std::string filedata = filereader.str();
24-
std::regex line_parser("([\\w\\.-]+)\\s+(\\d+)(?:(?:\r?\n)+|$)");
25-
for (std::regex_iterator<std::string::iterator> match = std::regex_iterator(filedata.begin(), filedata.end(), line_parser), end = std::regex_iterator<std::string::iterator>(); match != end; ++match)
26-
{
27-
whitelist.push_back(std::pair<std::string, std::string>(match->operator[](1).str(), match->operator[](2).str()));
28-
}
29-
if (whitelist.empty())
30-
{
31-
goto no_entries_error;
32-
}
33+
whitelist.push_back(std::pair<std::string, std::string>(match->operator[](1).str(), match->operator[](2).str()));
3334
}
34-
else
35+
if (whitelist.empty())
3536
{
36-
goto cant_read_error;
37+
return PARSE_NO_ENTRIES;
3738
}
38-
return 0;
3939
}
40-
no_entries_error:
41-
LUA->ThrowError("Didn't find any valid entries in whitelist file!");
42-
cant_read_error:
43-
LUA->ThrowError("Failed to read whitelist file!");
40+
else
41+
{
42+
return PARSE_CANT_READ;
43+
}
44+
return PARSE_SUCCESS;
4445
}
4546

4647
bool isSafe(const char* pNodeName, const char* pServiceName)

0 commit comments

Comments
 (0)