FEXServer: Fix unsafe vector insert/removal

Depending on the operation we will do a vector insert or removal while
iterating over the vector.

Fixes a use after free that asan found when insert caused the vector to
resize.
This commit is contained in:
Ryan Houdek committed 2022-08-05 23:00:13 -07:00
1 parent 9664d98ba5
commit b45f27c8d0
1 file changed
+7 -2
+7 -2
View File
@@ -434,6 +434,8 @@ namespace ProcessPipe {
ts.tv_sec = RequestTimeout;
int Result = ppoll(&PollFDs.at(0), PollFDs.size(), &ts, nullptr);
std::vector<struct pollfd> NewPollFDs{};
if (Result > 0) {
// Walk the FDs and see if we got any results
for (auto it = PollFDs.begin(); it != PollFDs.end(); ) {
@@ -448,8 +450,8 @@ namespace ProcessPipe {
socklen_t AddrSize{};
int NewFD = accept(ServerSocketFD, reinterpret_cast<struct sockaddr*>(&Addr), &AddrSize);
// Add the new client to the array
PollFDs.emplace_back(pollfd {
// Add the new client to the temporary array
NewPollFDs.emplace_back(pollfd {
.fd = NewFD,
.events = POLLIN | POLLPRI | POLLRDHUP | POLLREMOVE,
.revents = 0,
@@ -490,6 +492,9 @@ namespace ProcessPipe {
}
}
// Insert the new FDs to poll
PollFDs.insert(PollFDs.begin(), NewPollFDs.begin(), NewPollFDs.end());
LastDataTime = std::chrono::system_clock::now();
}
else {