diff --git a/src/io.cpp b/src/io.cpp index 584c10b61..1e5376146 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -327,13 +327,18 @@ maybe_t make_autoclose_pipes(const fd_set_t &fdset) { set_cloexec(pipes[0]); set_cloexec(pipes[1]); - auto read = move_fd_to_unused(autoclose_fd_t{pipes[0]}, fdset); - if (!read.valid()) return none(); + autoclose_fd_t read_end{pipes[0]}; + autoclose_fd_t write_end{pipes[1]}; - auto write = move_fd_to_unused(autoclose_fd_t{pipes[1]}, fdset); - if (!write.valid()) return none(); + // Ensure we have no conflicts. + if (!fdset.empty()) { + read_end = move_fd_to_unused(std::move(read_end), fdset); + if (!read_end.valid()) return none(); - return autoclose_pipes_t(std::move(read), std::move(write)); + write_end = move_fd_to_unused(std::move(write_end), fdset); + if (!write_end.valid()) return none(); + } + return autoclose_pipes_t(std::move(read_end), std::move(write_end)); } shared_ptr io_chain_t::io_for_fd(int fd) const { diff --git a/src/io.h b/src/io.h index 23b2952ef..5166b5c2d 100644 --- a/src/io.h +++ b/src/io.h @@ -39,6 +39,8 @@ struct fd_set_t { assert(fd >= 0 && "Invalid fd"); return static_cast(fd) < fds.size() && fds[fd]; } + + bool empty() const { return fds.empty(); } }; /// separated_buffer_t is composed of a sequence of elements, some of which may be explicitly