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 cycle more obvious by adding regular iterator into for() loop
and move Subprocess from running_ into finished_ in separate loop.
  • Loading branch information
dendy authored and Daniel Levin committed Aug 26, 2024
1 parent dcefb83 commit ad18d33
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/subprocess-posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -279,20 +279,23 @@ bool SubprocessSet::DoWork() {

nfds_t cur_nfd = 0;
for (vector<Subprocess*>::iterator i = running_.begin();
i != running_.end(); ) {
i != running_.end(); ++i) {
int fd = (*i)->fd_;
if (fd < 0)
continue;
assert(fd == fds[cur_nfd].fd);
if (fds[cur_nfd++].revents) {
if (fds[cur_nfd++].revents)
(*i)->OnPipeReady();
if ((*i)->Done()) {
finished_.push(*i);
i = running_.erase(i);
continue;
}
}

for (vector<Subprocess*>::iterator i = running_.begin();
i != running_.end(); ) {
if ((*i)->Done()) {
finished_.push(*i);
i = running_.erase(i);
} else {
++i;
}
++i;
}

return IsInterrupted();
Expand Down

0 comments on commit ad18d33

Please sign in to comment.