dbus-c++: Properly initialize and check file descriptors in DefaultMainLoop.
BUG=chromium:353524
TEST=dbus-c++ builds with latest Clang.
Change-Id: I3c8301f7676b404673eb9e60ca233c047da4dcb7
Reviewed-on: https://chromium-review.googlesource.com/190487
Reviewed-by: mukesh agrawal <quiche@chromium.org>
Tested-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/PRESUBMIT.cfg b/PRESUBMIT.cfg
new file mode 100644
index 0000000..3a45df2
--- /dev/null
+++ b/PRESUBMIT.cfg
@@ -0,0 +1,5 @@
+[Hook Overrides]
+stray_whitespace_check: false
+long_line_check: false
+cros_license_check: false
+tab_check: false
diff --git a/include/dbus-c++/eventloop.h b/include/dbus-c++/eventloop.h
index 8458756..2475fe5 100644
--- a/include/dbus-c++/eventloop.h
+++ b/include/dbus-c++/eventloop.h
@@ -152,7 +152,10 @@
virtual void dispatch();
+protected:
+
int _fdunlock[2];
+
private:
DefaultMutex _mutex_t;
diff --git a/src/eventloop-integration.cpp b/src/eventloop-integration.cpp
index 0d5d272..11eefa4 100644
--- a/src/eventloop-integration.cpp
+++ b/src/eventloop-integration.cpp
@@ -88,14 +88,15 @@
void BusDispatcher::leave()
{
_running = false;
-
- int ret = write(_fdunlock[1],"exit",strlen("exit"));
- if (ret == -1) {
- char buffer[128]; // buffer copied in Error constructor
- throw Error("PipeError:errno", strerror_r(errno,
- buffer,
- sizeof(buffer)));
- }
+ if (_fdunlock[1] >= 0) {
+ int ret = write(_fdunlock[1],"exit",strlen("exit"));
+ if (ret == -1) {
+ char buffer[128]; // buffer copied in Error constructor
+ throw Error("PipeError:errno", strerror_r(errno,
+ buffer,
+ sizeof(buffer)));
+ }
+ }
close(_fdunlock[1]);
close(_fdunlock[0]);
}
diff --git a/src/eventloop.cpp b/src/eventloop.cpp
index 1bc8cd5..81861f0 100644
--- a/src/eventloop.cpp
+++ b/src/eventloop.cpp
@@ -106,6 +106,8 @@
DefaultMainLoop::DefaultMainLoop()
{
+ _fdunlock[0] = -1;
+ _fdunlock[1] = -1;
}
DefaultMainLoop::~DefaultMainLoop()
@@ -145,7 +147,7 @@
int nfd = _watches.size();
- if(_fdunlock)
+ if (_fdunlock[0] >= 0 && _fdunlock[1] >= 0)
{
nfd=nfd+2;
}
@@ -166,7 +168,8 @@
}
}
- if(_fdunlock){
+ if (_fdunlock[0] >= 0 && _fdunlock[1] >= 0)
+ {
fds[nfd].fd = _fdunlock[0];
fds[nfd].events = POLLIN | POLLOUT | POLLPRI ;
fds[nfd].revents = 0;