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

Commit 92ee52cc authored by Josh Gao's avatar Josh Gao
Browse files

base: don't overwrite errno in unique_fd::~unique_fd.

unique_fd's destructor potentially mangling errno makes it difficult to
use correctly in code that sets errno (or, in reality, it makes it so
that errno values get randomly stomped upon if close actually sets
errno, because no one accounts for this case).

Preserve errno ourselves to avoid this.

Test: treehugger
Change-Id: Ib06e6f65866d86fff4032b2311021eaf9226a1af
parent 2dc8b4ce
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -17,10 +17,10 @@
#pragma once

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>

#if !defined(_WIN32)
#include <dirent.h>
#include <sys/socket.h>
#endif

@@ -114,6 +114,8 @@ class unique_fd_impl final {

 private:
  void reset(int new_value, void* previous_tag) {
    int previous_errno = errno;

    if (fd_ != -1) {
      close(fd_, this);
    }
@@ -122,6 +124,8 @@ class unique_fd_impl final {
    if (new_value != -1) {
      tag(new_value, previous_tag, this);
    }

    errno = previous_errno;
  }

  int fd_ = -1;