summaryrefslogtreecommitdiff
path: root/libs/androidfw/AssetManager2.cpp
diff options
context:
space:
mode:
author George Burgess IV <gbiv@google.com> 2017-07-25 15:00:04 -0700
committer George Burgess IV <gbiv@google.com> 2017-07-26 17:39:32 -0700
commit09b119fb7d0494e9afd2bf2ec774532fcafe754e (patch)
tree06908db0ed58bce146052866dbb8d2693368e28d /libs/androidfw/AssetManager2.cpp
parent6a65b75e479a9de3d6d8a066eeb4d1715b18c03b (diff)
AssetManager2: Fix a memory leak
Caught by the static analyzer: frameworks/base/libs/androidfw/AssetManager2.cpp:580:9: warning: Potential leak of memory pointed to by 'new_bag' [clang-analyzer-unix.Malloc] Bug: 27101951 Test: mma. Memory leak warning is gone. Change-Id: I532585d4dd376cec1abf4358f26d23f5ae3231cf
Diffstat (limited to 'libs/androidfw/AssetManager2.cpp')
-rw-r--r--libs/androidfw/AssetManager2.cpp17
1 files changed, 8 insertions, 9 deletions
diff --git a/libs/androidfw/AssetManager2.cpp b/libs/androidfw/AssetManager2.cpp
index 5667f9283241..ab7e14de48fb 100644
--- a/libs/androidfw/AssetManager2.cpp
+++ b/libs/androidfw/AssetManager2.cpp
@@ -533,8 +533,8 @@ const ResolvedBag* AssetManager2::GetBag(uint32_t resid) {
// Create the max possible entries we can make. Once we construct the bag,
// we will realloc to fit to size.
const size_t max_count = parent_bag->entry_count + dtohl(map->count);
- ResolvedBag* new_bag = reinterpret_cast<ResolvedBag*>(
- malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))));
+ util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
+ malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))};
ResolvedBag::Entry* new_entry = new_bag->entries;
const ResolvedBag::Entry* parent_entry = parent_bag->entries;
@@ -601,15 +601,14 @@ const ResolvedBag* AssetManager2::GetBag(uint32_t resid) {
// Resize the resulting array to fit.
const size_t actual_count = new_entry - new_bag->entries;
if (actual_count != max_count) {
- new_bag = reinterpret_cast<ResolvedBag*>(
- realloc(new_bag, sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry))));
+ new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc(
+ new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry)))));
}
- util::unique_cptr<ResolvedBag> final_bag{new_bag};
- final_bag->type_spec_flags = flags;
- final_bag->entry_count = static_cast<uint32_t>(actual_count);
- ResolvedBag* result = final_bag.get();
- cached_bags_[resid] = std::move(final_bag);
+ new_bag->type_spec_flags = flags;
+ new_bag->entry_count = static_cast<uint32_t>(actual_count);
+ ResolvedBag* result = new_bag.get();
+ cached_bags_[resid] = std::move(new_bag);
return result;
}