mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-13 13:39:02 +00:00
lint: Use early exit/continue
This commit is contained in:
parent
2c38978115
commit
d4fb9a0e65
1 changed files with 28 additions and 28 deletions
56
src/io.cpp
56
src/io.cpp
|
@ -165,35 +165,35 @@ 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
|
|
||||||
// anything is closed; this forces the kernel to give us a new one (or report fd
|
|
||||||
// exhaustion).
|
|
||||||
int tmp_fd;
|
|
||||||
do {
|
|
||||||
tmp_fd = dup(fd);
|
|
||||||
} while (tmp_fd < 0 && errno == EINTR);
|
|
||||||
|
|
||||||
assert(tmp_fd != fd);
|
|
||||||
if (tmp_fd < 0) {
|
|
||||||
// Likely fd exhaustion.
|
|
||||||
new_fd = -1;
|
|
||||||
} else {
|
|
||||||
// Ok, we have a new candidate fd. Recurse. If we get a valid fd, either it's the same
|
|
||||||
// as 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.
|
|
||||||
set_cloexec(tmp_fd);
|
|
||||||
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
|
|
||||||
// one.
|
|
||||||
assert(new_fd != fd);
|
|
||||||
int saved_errno = errno;
|
|
||||||
exec_close(fd);
|
|
||||||
errno = saved_errno;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 exhaustion).
|
||||||
|
int new_fd = fd;
|
||||||
|
int tmp_fd;
|
||||||
|
do {
|
||||||
|
tmp_fd = dup(fd);
|
||||||
|
} while (tmp_fd < 0 && errno == EINTR);
|
||||||
|
|
||||||
|
assert(tmp_fd != fd);
|
||||||
|
if (tmp_fd < 0) {
|
||||||
|
// Likely fd exhaustion.
|
||||||
|
new_fd = -1;
|
||||||
|
} else {
|
||||||
|
// Ok, we have a new candidate fd. Recurse. If we get a valid fd, either it's the same as
|
||||||
|
// 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.
|
||||||
|
set_cloexec(tmp_fd);
|
||||||
|
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 one.
|
||||||
|
assert(new_fd != fd);
|
||||||
|
int saved_errno = errno;
|
||||||
|
exec_close(fd);
|
||||||
|
errno = saved_errno;
|
||||||
return new_fd;
|
return new_fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue