Skip to content

Commit 573102c

Browse files
committed
Fixed lua error usage
1 parent 2de039b commit 573102c

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

source/socket.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ int parseWhitelist(GarrysMod::Lua::ILuaBase* LUA);
77
GMOD_MODULE_OPEN( )
88
{
99
#ifdef USE_WHITELIST
10+
// This might long-jump via lua error so watch out!
1011
if (parseWhitelist(LUA))
1112
{
1213
return 0;

source/whitelist.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,33 @@ std::vector<std::pair<std::string, std::string> > whitelist;
1414

1515
int parseWhitelist(GarrysMod::Lua::ILuaBase* LUA)
1616
{
17-
std::ifstream input(whitelistDir);
18-
if(input)
1917
{
20-
std::stringstream filereader;
21-
filereader << input.rdbuf();
22-
std::string filedata = filereader.str();
23-
std::regex line_parser("([\\w\\.-]+)\\s+(\\d+)(?:(?:\r?\n)+|$)");
24-
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)
18+
std::ifstream input(whitelistDir);
19+
if (input)
2520
{
26-
whitelist.push_back(std::pair<std::string, std::string>(match->operator[](1).str(), match->operator[](2).str()));
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+
}
2733
}
28-
if (whitelist.empty())
34+
else
2935
{
30-
LUA->PushString("Didn't find any valid entries in whitelist file!");
31-
LUA->Error();
32-
return 1;
36+
goto cant_read_error;
3337
}
38+
return 0;
3439
}
35-
else
36-
{
37-
LUA->PushString("Failed to read whitelist file!");
38-
LUA->Error();
39-
return 1;
40-
}
41-
return 0;
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!");
4244
}
4345

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

0 commit comments

Comments
 (0)