Fix static analyzer warnings
runtime/base/variant_map.h: Potential leak of memory pointed to by field
'first' + 'second'
The static analyzer is smart enough to figure out that key.Clone()
allocates memory, but can't figure out that Remove(key) will remove the
key from the map. For more, please see the discussion on patch set 1 of
variant_map.h in https://android-review.googlesource.com/#/c/416101 .
Bug: 32619234
Test: test-art-host. Rebuilt ART with the analyzer to verify that these
issues are gone.
Change-Id: I8ebec5dc0d1f8863b316515d7d16a1e1a625132d
diff --git a/runtime/base/variant_map.h b/runtime/base/variant_map.h
index fdb60c4..d87df87 100644
--- a/runtime/base/variant_map.h
+++ b/runtime/base/variant_map.h
@@ -22,6 +22,7 @@
#include <type_traits>
#include <utility>
+#include "android-base/logging.h"
#include "base/stl_util_identity.h"
namespace art {
@@ -278,7 +279,8 @@
auto* new_value = new TValue(value);
Remove(key);
- storage_map_.insert({{key.Clone(), new_value}});
+ bool inserted = storage_map_.insert({key.Clone(), new_value}).second;
+ DCHECK(inserted); // ensure key.Clone() does not leak memory.
}
// Set a value for a given key, only if there was no previous value before.