summaryrefslogtreecommitdiff
path: root/libsysutils/src/SocketClient.cpp
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@android.com>2011-03-17 17:14:46 -0700
committerBrad Fitzpatrick <bradfitz@android.com>2011-03-17 17:14:46 -0700
commit4be4e69f0128b7d9b0a29651ef4b79d806ae3ce7 (patch)
tree7ea32b884c407264ff50b4b857e51b94a5fcf825 /libsysutils/src/SocketClient.cpp
parent51101e86dce3c512a915308274406968aa5d0760 (diff)
Fix potential race introduced in Icd7f5f03
Digit wrote: "You probably don't want to close the socket here without updating c->socket as well. Otherwise, another thread holding a handle to the client after the c->decRef() could end up sending a message to a different socket, if the file descriptor index is reused by another client in the meantime." Change-Id: Icdefb5ffc0c7607325d7db761e1f04e5d868bfb7
Diffstat (limited to 'libsysutils/src/SocketClient.cpp')
-rw-r--r--libsysutils/src/SocketClient.cpp31
1 files changed, 17 insertions, 14 deletions
diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp
index 6d4dff45d..90ca52e74 100644
--- a/libsysutils/src/SocketClient.cpp
+++ b/libsysutils/src/SocketClient.cpp
@@ -104,20 +104,23 @@ int SocketClient::sendData(const void* data, int len) {
}
void SocketClient::incRef() {
- pthread_mutex_lock(&mRefCountMutex);
- mRefCount++;
- pthread_mutex_unlock(&mRefCountMutex);
+ pthread_mutex_lock(&mRefCountMutex);
+ mRefCount++;
+ pthread_mutex_unlock(&mRefCountMutex);
}
-void SocketClient::decRef() {
- bool deleteSelf = false;
- pthread_mutex_lock(&mRefCountMutex);
- mRefCount--;
- if (mRefCount == 0) {
- deleteSelf = true;
- }
- pthread_mutex_unlock(&mRefCountMutex);
- if (deleteSelf) {
- delete this;
- }
+bool SocketClient::decRef() {
+ bool deleteSelf = false;
+ pthread_mutex_lock(&mRefCountMutex);
+ mRefCount--;
+ if (mRefCount == 0) {
+ deleteSelf = true;
+ } else if (mRefCount < 0) {
+ SLOGE("SocketClient refcount went negative!");
+ }
+ pthread_mutex_unlock(&mRefCountMutex);
+ if (deleteSelf) {
+ delete this;
+ }
+ return deleteSelf;
}