summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorJosh Gao <jmgao@google.com>2019-01-25 16:22:41 -0800
committerJosh Gao <jmgao@google.com>2019-01-30 15:18:56 -0800
commit92ee52cc38ca1a06b7b0851643314b0830f0ece9 (patch)
tree63620c99b06c38346d34a8721b13f3933f491f96 /base
parent2dc8b4cec8c2900f77a0dc4ff7219401e3552d7a (diff)
downloadsystem_core-92ee52cc38ca1a06b7b0851643314b0830f0ece9.tar.gz
system_core-92ee52cc38ca1a06b7b0851643314b0830f0ece9.tar.bz2
system_core-92ee52cc38ca1a06b7b0851643314b0830f0ece9.zip
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
Diffstat (limited to 'base')
-rw-r--r--base/include/android-base/unique_fd.h6
1 files changed, 5 insertions, 1 deletions
diff --git a/base/include/android-base/unique_fd.h b/base/include/android-base/unique_fd.h
index 2c890b42d..83213e9c4 100644
--- a/base/include/android-base/unique_fd.h
+++ b/base/include/android-base/unique_fd.h
@@ -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;