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

Commit 9c790312 authored by Yifan Hong's avatar Yifan Hong
Browse files

Fix fds libdm_test

Failed because of double free of fds.
Test: run it

Change-Id: I25d7d590ca52d57fb14a5483ff8751127f6a48a6
parent 2becdb65
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -64,7 +64,8 @@ class LoopDevice {
  public:
    // Create a loop device for the given file descriptor. It is closed when
    // LoopDevice is destroyed only if auto_close is true.
    LoopDevice(int fd, const std::chrono::milliseconds& timeout_ms, bool auto_close = false);
    LoopDevice(android::base::borrowed_fd fd, const std::chrono::milliseconds& timeout_ms,
               bool auto_close = false);
    // Create a loop device for the given file path. It will be opened for
    // reading and writing and closed when the loop device is detached.
    LoopDevice(const std::string& path, const std::chrono::milliseconds& timeout_ms);
@@ -81,8 +82,8 @@ class LoopDevice {
  private:
    void Init(const std::chrono::milliseconds& timeout_ms);

    android::base::unique_fd fd_;
    bool owns_fd_;
    android::base::borrowed_fd fd_;
    android::base::unique_fd owned_fd_;
    std::string device_;
    LoopControl control_;
    bool valid_ = false;
+11 −9
Original line number Diff line number Diff line
@@ -133,18 +133,23 @@ bool LoopControl::EnableDirectIo(int fd) {
    return true;
}

LoopDevice::LoopDevice(int fd, const std::chrono::milliseconds& timeout_ms, bool auto_close)
    : fd_(fd), owns_fd_(auto_close) {
LoopDevice::LoopDevice(android::base::borrowed_fd fd, const std::chrono::milliseconds& timeout_ms,
                       bool auto_close)
    : fd_(fd), owned_fd_(-1) {
    if (auto_close) {
        owned_fd_.reset(fd.get());
    }
    Init(timeout_ms);
}

LoopDevice::LoopDevice(const std::string& path, const std::chrono::milliseconds& timeout_ms)
    : fd_(-1), owns_fd_(true) {
    fd_.reset(open(path.c_str(), O_RDWR | O_CLOEXEC));
    if (fd_ < -1) {
    : fd_(-1), owned_fd_(-1) {
    owned_fd_.reset(open(path.c_str(), O_RDWR | O_CLOEXEC));
    if (owned_fd_ == -1) {
        PLOG(ERROR) << "open failed for " << path;
        return;
    }
    fd_ = owned_fd_;
    Init(timeout_ms);
}

@@ -152,13 +157,10 @@ LoopDevice::~LoopDevice() {
    if (valid()) {
        control_.Detach(device_);
    }
    if (!owns_fd_) {
        (void)fd_.release();
    }
}

void LoopDevice::Init(const std::chrono::milliseconds& timeout_ms) {
    valid_ = control_.Attach(fd_, timeout_ms, &device_);
    valid_ = control_.Attach(fd_.get(), timeout_ms, &device_);
}

}  // namespace dm