Fix wrong bundle_foreach implementation 72/284572/1
authorChanggyu Choi <changyu.choi@samsung.com>
Mon, 21 Nov 2022 01:14:40 +0000 (10:14 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Mon, 21 Nov 2022 01:14:40 +0000 (10:14 +0900)
if data is removed through bundle_foreach(),
A crash may occur.
This patch resolves this by passing the iterator to the next before calling the callback using the iterator.

Change-Id: I3f04418f47bc17247e857362b462e616c2136fa0
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
src/stub.cc

index fdfc9dc..540ca4c 100644 (file)
@@ -160,10 +160,12 @@ extern "C" EXPORT_API void bundle_foreach(bundle* b,
   }
 
   auto* h = reinterpret_cast<Bundle*>(b);
   }
 
   auto* h = reinterpret_cast<Bundle*>(b);
-  for (const auto& kv : h->GetMap()) {
-    auto& key_info = kv.second;
+  auto it = h->GetMap().begin();
+  while (it != h->GetMap().end()) {
+    auto& key_info = it->second;
+    ++it;
     callback(key_info->GetKey().c_str(), key_info->GetType(),
     callback(key_info->GetKey().c_str(), key_info->GetType(),
-        reinterpret_cast<bundle_keyval_t*>(key_info.get()), user_data);
+             reinterpret_cast<bundle_keyval_t*>(key_info.get()), user_data);
   }
 
   set_last_result(BUNDLE_ERROR_NONE);
   }
 
   set_last_result(BUNDLE_ERROR_NONE);