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
This commit is contained in:
Josh Gao 2019-01-25 16:22:41 -08:00
parent 2dc8b4cec8
commit 92ee52cc38
1 changed files with 5 additions and 1 deletions

View File

@ -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;