This is an automated email from the ASF dual-hosted git repository.
leander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git
The following commit(s) were added to refs/heads/master by this push:
new 9fd7e21e Fix scoped_refptr leave ptr_ uninit when move construct by
nullptr (#2491)
9fd7e21e is described below
commit 9fd7e21ef78952967e94785180164aaf2821a892
Author: Li Zhiyuan <[email protected]>
AuthorDate: Wed Jan 3 14:22:15 2024 +0800
Fix scoped_refptr leave ptr_ uninit when move construct by nullptr (#2491)
* Fix scoped_refptr leave ptr_ uninit when move construct by nullptr
---------
Co-authored-by: 扬宁 <[email protected]>
---
src/butil/memory/ref_counted.h | 14 +++++---------
test/ref_counted_unittest.cc | 13 +++++++++++++
2 files changed, 18 insertions(+), 9 deletions(-)
diff --git a/src/butil/memory/ref_counted.h b/src/butil/memory/ref_counted.h
index 8fbb5d0d..fb8bc72d 100644
--- a/src/butil/memory/ref_counted.h
+++ b/src/butil/memory/ref_counted.h
@@ -286,20 +286,16 @@ class scoped_refptr {
}
scoped_refptr(scoped_refptr<T>&& r) noexcept {
- if (r.ptr_){
- ptr_ = r.ptr_;
- r.ptr_ = nullptr;
- }
+ ptr_ = r.ptr_;
+ r.ptr_ = nullptr;
}
template <typename U>
scoped_refptr(scoped_refptr<U>&& r) noexcept {
- if (r.ptr_){
- ptr_ = r.ptr_;
- r.ptr_ = nullptr;
- }
+ ptr_ = r.ptr_;
+ r.ptr_ = nullptr;
}
-
+
~scoped_refptr() {
if (ptr_)
ptr_->Release();
diff --git a/test/ref_counted_unittest.cc b/test/ref_counted_unittest.cc
index 8de76002..7415ba3e 100644
--- a/test/ref_counted_unittest.cc
+++ b/test/ref_counted_unittest.cc
@@ -82,3 +82,16 @@ TEST(RefCountedUnitTest, ScopedRefPtrBooleanOperations) {
EXPECT_NE(raw_p, p2);
EXPECT_EQ(p1, p2);
}
+
+TEST(RefCountedUnitTest, ScopedRefPtrMoveCtor)
+{
+ scoped_refptr<SelfAssign> p1 = new SelfAssign;
+ EXPECT_TRUE(p1);
+
+ scoped_refptr<SelfAssign> p2(std::move(p1));
+ EXPECT_TRUE(p2);
+ EXPECT_FALSE(p1);
+
+ scoped_refptr<SelfAssign> p3(std::move(p1));
+ EXPECT_FALSE(p3);
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]