art: Fix bug in VariantMap::Set
Bug: 19295410
Change-Id: I7827583846d710698c0e7bc0ec1a2c3bf901bd50
diff --git a/runtime/base/variant_map.h b/runtime/base/variant_map.h
index cf7977e..c9718fc 100644
--- a/runtime/base/variant_map.h
+++ b/runtime/base/variant_map.h
@@ -271,8 +271,11 @@
// Set a value for a given key, overwriting the previous value if any.
template <typename TValue>
void Set(const TKey<TValue>& key, const TValue& value) {
+ // Clone the value first, to protect against &value == GetValuePtr(key).
+ auto* new_value = new TValue(value);
+
Remove(key);
- storage_map_.insert({{key.Clone(), new TValue(value)}});
+ storage_map_.insert({{key.Clone(), new_value}});
}
// Set a value for a given key, only if there was no previous value before.