Fix locking on string init map.

Related to: https://android-review.googlesource.com/#/c/171621

Change-Id: Ib6dcf9914f490f1d744c0abe1f520e5b307c7acd
diff --git a/runtime/interpreter/interpreter_common.cc b/runtime/interpreter/interpreter_common.cc
index 02ec90c..68d56f5 100644
--- a/runtime/interpreter/interpreter_common.cc
+++ b/runtime/interpreter/interpreter_common.cc
@@ -696,22 +696,20 @@
     // new result of the StringFactory. Use the verifier to find this set of registers.
     ArtMethod* method = shadow_frame.GetMethod();
     MethodReference method_ref = method->ToMethodReference();
-    SafeMap<uint32_t, std::set<uint32_t>> string_init_map;
-    SafeMap<uint32_t, std::set<uint32_t>>* string_init_map_ptr;
+    SafeMap<uint32_t, std::set<uint32_t>>* string_init_map_ptr = nullptr;
     MethodRefToStringInitRegMap& method_to_string_init_map = Runtime::Current()->GetStringInitMap();
-    MethodRefToStringInitRegMap::iterator it;
     {
       MutexLock mu(self, *Locks::interpreter_string_init_map_lock_);
-      it = method_to_string_init_map.find(method_ref);
-    }
-    if (it == method_to_string_init_map.end()) {
-      string_init_map = std::move(verifier::MethodVerifier::FindStringInitMap(method));
-      {
-        MutexLock mu(self, *Locks::interpreter_string_init_map_lock_);
-        method_to_string_init_map.Overwrite(method_ref, string_init_map);
+      auto it = method_to_string_init_map.find(method_ref);
+      if (it != method_to_string_init_map.end()) {
+        string_init_map_ptr = &it->second;
       }
-      string_init_map_ptr = &string_init_map;
-    } else {
+    }
+    if (string_init_map_ptr == nullptr) {
+      SafeMap<uint32_t, std::set<uint32_t>> string_init_map =
+          verifier::MethodVerifier::FindStringInitMap(method);
+      MutexLock mu(self, *Locks::interpreter_string_init_map_lock_);
+      auto it = method_to_string_init_map.Overwrite(method_ref, string_init_map);
       string_init_map_ptr = &it->second;
     }
     if (string_init_map_ptr->size() != 0) {
diff --git a/runtime/safe_map.h b/runtime/safe_map.h
index 402c7e9..04549c7 100644
--- a/runtime/safe_map.h
+++ b/runtime/safe_map.h
@@ -104,12 +104,13 @@
   // Used to insert a new mapping or overwrite an existing mapping. Note that if the value type
   // of this container is a pointer, any overwritten pointer will be lost and if this container
   // was the owner, you have a leak.
-  void Overwrite(const K& k, const V& v) {
+  iterator Overwrite(const K& k, const V& v) {
     std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v));
     if (!result.second) {
       // Already there - update the value for the existing key
       result.first->second = v;
     }
+    return result.first;
   }
 
   bool Equals(const Self& rhs) const {