Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 499601b9 authored by Josh Gao's avatar Josh Gao
Browse files

adb: fix double close in wait-for-*.

unique_fd's implicit conversion to int allows the following code to
compile without error, leading to a double close:

  std::function<void(unique_fd)> func = [](int x) { close(x); };
  func(unique_fd(42));

Test: treehugger
Change-Id: I948ecda3a12738b3af6444fe2902d2f7b80e1b4c
parent fc174928
Loading
Loading
Loading
Loading
+4 −6
Original line number Original line Diff line number Diff line
@@ -100,7 +100,7 @@ struct state_info {
    ConnectionState state;
    ConnectionState state;
};
};


static void wait_for_state(int fd, state_info* sinfo) {
static void wait_for_state(unique_fd fd, state_info* sinfo) {
    D("wait_for_state %d", sinfo->state);
    D("wait_for_state %d", sinfo->state);


    while (true) {
    while (true) {
@@ -122,7 +122,7 @@ static void wait_for_state(int fd, state_info* sinfo) {
        }
        }


        if (!is_ambiguous) {
        if (!is_ambiguous) {
            adb_pollfd pfd = {.fd = fd, .events = POLLIN};
            adb_pollfd pfd = {.fd = fd.get(), .events = POLLIN};
            int rc = adb_poll(&pfd, 1, 100);
            int rc = adb_poll(&pfd, 1, 100);
            if (rc < 0) {
            if (rc < 0) {
                SendFail(fd, error);
                SendFail(fd, error);
@@ -140,7 +140,6 @@ static void wait_for_state(int fd, state_info* sinfo) {
        }
        }
    }
    }


    adb_close(fd);
    D("wait_for_state is done");
    D("wait_for_state is done");
}
}


@@ -239,9 +238,8 @@ asocket* host_service_to_socket(std::string_view name, std::string_view serial,
            return nullptr;
            return nullptr;
        }
        }


        unique_fd fd = create_service_thread("wait", [sinfo](int fd) {
        unique_fd fd = create_service_thread(
            wait_for_state(fd, sinfo.get());
                "wait", [sinfo](unique_fd fd) { wait_for_state(std::move(fd), sinfo.get()); });
        });
        return create_local_socket(std::move(fd));
        return create_local_socket(std::move(fd));
    } else if (ConsumePrefix(&name, "connect:")) {
    } else if (ConsumePrefix(&name, "connect:")) {
        std::string host(name);
        std::string host(name);