Skip to content

Commit

Permalink
Fix infinite loop in SubprocessSet::DoWork()
Browse files Browse the repository at this point in the history
In case fd is invalid loop will be infinite because iterator is not
incremented.

Make loop consistent between both variants of SubprocessSet::DoWork()
(with and withoug USE_PPOLL) by removing 'continue' and always advancing
iterator in the end of the cycle.
  • Loading branch information
dendy authored and Daniel Levin committed Aug 28, 2024
1 parent f220dc5 commit a6da182
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/subprocess-posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ bool SubprocessSet::DoWork() {
for (vector<Subprocess*>::iterator i = running_.begin();
i != running_.end(); ++i) {
int fd = (*i)->fd_;
if (fd < 0)
continue;
pollfd pfd = { fd, POLLIN | POLLPRI, 0 };
fds.push_back(pfd);
++nfds;
if (fd >= 0) {
pollfd pfd = { fd, POLLIN | POLLPRI, 0 };
fds.push_back(pfd);
++nfds;
}
}

interrupted_ = 0;
Expand All @@ -281,18 +281,18 @@ bool SubprocessSet::DoWork() {
for (vector<Subprocess*>::iterator i = running_.begin();
i != running_.end(); ) {
int fd = (*i)->fd_;
if (fd < 0)
continue;
assert(fd == fds[cur_nfd].fd);
if (fds[cur_nfd++].revents) {
(*i)->OnPipeReady();
if ((*i)->Done()) {
finished_.push(*i);
i = running_.erase(i);
continue;
if (fd >= 0) {
assert(fd == fds[cur_nfd].fd);
if (fds[cur_nfd++].revents) {
(*i)->OnPipeReady();
}
}
++i;
if ((*i)->Done()) {
finished_.push(*i);
i = running_.erase(i);
} else {
++i;
}
}

return IsInterrupted();
Expand Down Expand Up @@ -333,13 +333,13 @@ bool SubprocessSet::DoWork() {
int fd = (*i)->fd_;
if (fd >= 0 && FD_ISSET(fd, &set)) {
(*i)->OnPipeReady();
if ((*i)->Done()) {
finished_.push(*i);
i = running_.erase(i);
continue;
}
}
++i;
if ((*i)->Done()) {
finished_.push(*i);
i = running_.erase(i);
} else {
++i;
}
}

return IsInterrupted();
Expand Down

0 comments on commit a6da182

Please sign in to comment.