lint: Use early exit/continue

This commit is contained in:
Kurtis Rader 2016-10-30 21:18:59 -07:00
parent 2c38978115
commit d4fb9a0e65

View file

@ -165,11 +165,13 @@ void io_print(const io_chain_t &chain)
/// created is marked close-on-exec. Returns -1 on failure (in which case the given fd is still /// created is marked close-on-exec. Returns -1 on failure (in which case the given fd is still
/// closed). /// closed).
static int move_fd_to_unused(int fd, const io_chain_t &io_chain) { static int move_fd_to_unused(int fd, const io_chain_t &io_chain) {
int new_fd = fd; if (fd < 0 || io_chain.get_io_for_fd(fd).get() == NULL) {
if (fd >= 0 && io_chain.get_io_for_fd(fd).get() != NULL) { return fd;
}
// We have fd >= 0, and it's a conflict. dup it and recurse. Note that we recurse before // We have fd >= 0, and it's a conflict. dup it and recurse. Note that we recurse before
// anything is closed; this forces the kernel to give us a new one (or report fd // anything is closed; this forces the kernel to give us a new one (or report fd exhaustion).
// exhaustion). int new_fd = fd;
int tmp_fd; int tmp_fd;
do { do {
tmp_fd = dup(fd); tmp_fd = dup(fd);
@ -180,20 +182,18 @@ static int move_fd_to_unused(int fd, const io_chain_t &io_chain) {
// Likely fd exhaustion. // Likely fd exhaustion.
new_fd = -1; new_fd = -1;
} else { } else {
// Ok, we have a new candidate fd. Recurse. If we get a valid fd, either it's the same // Ok, we have a new candidate fd. Recurse. If we get a valid fd, either it's the same as
// as what we gave it, or it's a new fd and what we gave it has been closed. If we get a // what we gave it, or it's a new fd and what we gave it has been closed. If we get a
// negative value, the fd also has been closed. // negative value, the fd also has been closed.
set_cloexec(tmp_fd); set_cloexec(tmp_fd);
new_fd = move_fd_to_unused(tmp_fd, io_chain); new_fd = move_fd_to_unused(tmp_fd, io_chain);
} }
// We're either returning a new fd or an error. In both cases, we promise to close the old // We're either returning a new fd or an error. In both cases, we promise to close the old one.
// one.
assert(new_fd != fd); assert(new_fd != fd);
int saved_errno = errno; int saved_errno = errno;
exec_close(fd); exec_close(fd);
errno = saved_errno; errno = saved_errno;
}
return new_fd; return new_fd;
} }