summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Kaiser2016-08-01 14:46:22 -0500
committerGreg Kaiser2016-08-05 08:28:10 -0500
commitdd55734dda700a5617b2651bd1b4678e567bb75e (patch)
treea79a21604264fd636fbd53d1cbc67cd3d72f3ea7 /libutils/SharedBuffer.cpp
parent23a5b3921d7889e7b6b74d100c73ab8acfa2a1a3 (diff)
downloadplatform-system-core-dd55734dda700a5617b2651bd1b4678e567bb75e.tar.gz
platform-system-core-dd55734dda700a5617b2651bd1b4678e567bb75e.tar.xz
platform-system-core-dd55734dda700a5617b2651bd1b4678e567bb75e.zip
SharedBuffer: Fix bug in return value of release()
Since the equality operator '==' has higher precedence than the assignment operator '=', we were assigning 'prev' to the result of our comparison and not the result of mRefs.fetch_sub(). This means that 'prev' would only receive the values 0 or 1. In the cases where fetch_sub() returned 0 or 1, we were happening to get the correct value. But if fetch_sub() was greator than 1, we would return to the user 0, instead of the previous reference count. We fix this by properly adding parentheses. We also adjust the whitespace a little to hopefully make the groupings of the logic easier to see. Change-Id: Ib129798a7076854b9ca4f6385c42edbf4fb75e57
Diffstat (limited to 'libutils/SharedBuffer.cpp')
-rw-r--r--libutils/SharedBuffer.cpp5
1 files changed, 3 insertions, 2 deletions
diff --git a/libutils/SharedBuffer.cpp b/libutils/SharedBuffer.cpp
index a8a9fb415..6c8c7d3dc 100644
--- a/libutils/SharedBuffer.cpp
+++ b/libutils/SharedBuffer.cpp
@@ -112,8 +112,9 @@ void SharedBuffer::acquire() const {
112int32_t SharedBuffer::release(uint32_t flags) const 112int32_t SharedBuffer::release(uint32_t flags) const
113{ 113{
114 int32_t prev = 1; 114 int32_t prev = 1;
115 if (onlyOwner() || ((prev = mRefs.fetch_sub(1, std::memory_order_release) == 1) 115 if (onlyOwner()
116 && (atomic_thread_fence(std::memory_order_acquire), true))) { 116 || (((prev = mRefs.fetch_sub(1, std::memory_order_release)) == 1)
117 && (atomic_thread_fence(std::memory_order_acquire), true))) {
117 mRefs.store(0, std::memory_order_relaxed); 118 mRefs.store(0, std::memory_order_relaxed);
118 if ((flags & eKeepStorage) == 0) { 119 if ((flags & eKeepStorage) == 0) {
119 free(const_cast<SharedBuffer*>(this)); 120 free(const_cast<SharedBuffer*>(this));