Fix copy constructor for Bundle 95/311695/5
authorjh9216.park <jh9216.park@samsung.com>
Fri, 24 May 2024 01:03:03 +0000 (21:03 -0400)
committerjh9216.park <jh9216.park@samsung.com>
Fri, 24 May 2024 04:19:02 +0000 (00:19 -0400)
- Copied bundle using bundle_dup() should not share data from origin
object

Change-Id: I71c0c69159a11195c79ff486627453d105851677
Signed-off-by: jh9216.park <jh9216.park@samsung.com>
src/bundle-internal.cc

index f63dfe8..68ccd8e 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <algorithm>
 #include <cstring>
+#include <memory>
 #include <utility>
 
 #include "include/bundle.h"
@@ -52,14 +53,24 @@ Bundle::Bundle(int argc, char** argv) {
 }
 
 Bundle::Bundle(const Bundle& b) {
-  map_ = b.map_;
-  list_ = b.list_;
+  for (const auto& [key, val] : b.map_) {
+    const KeyInfo& info = *val;
+    auto new_val = std::make_shared<KeyInfo>(info);
+    map_[key] = new_val;
+    list_.push_back(std::move(new_val));
+  }
 }
 
 Bundle& Bundle::operator = (const Bundle& b) {
   if (this != &b) {
-    map_ = b.map_;
-    list_ = b.list_;
+    map_.clear();
+    list_.clear();
+    for (const auto& [key, val] : b.map_) {
+      const KeyInfo& info = *val;
+      auto new_val = std::make_shared<KeyInfo>(info);
+      map_[key] = new_val;
+      list_.push_back(std::move(new_val));
+    }
   }
   return *this;
 }
@@ -91,7 +102,7 @@ bool Bundle::operator == (const Bundle& b) {
       return false;
 
     auto& rhs = iter->second;
-    if (!(lhs == rhs))
+    if (!(*lhs == *rhs))
       return false;
   }