Merge "Move MapInfo objects in to frame data."
diff --git a/libunwindstack/MapInfo.cpp b/libunwindstack/MapInfo.cpp
index 752df2b..6193015 100644
--- a/libunwindstack/MapInfo.cpp
+++ b/libunwindstack/MapInfo.cpp
@@ -236,12 +236,15 @@
   // the r-x section, which is not quite the right information.
   set_elf_start_offset(prev_real_map->offset());
 
-  MemoryRanges* ranges = new MemoryRanges;
-  ranges->Insert(new MemoryRange(process_memory, prev_real_map->start(),
-                                 prev_real_map->end() - prev_real_map->start(), 0));
-  ranges->Insert(new MemoryRange(process_memory, start(), end() - start(), elf_offset()));
-
-  return ranges;
+  std::unique_ptr<MemoryRanges> ranges(new MemoryRanges);
+  if (!ranges->Insert(new MemoryRange(process_memory, prev_real_map->start(),
+                                      prev_real_map->end() - prev_real_map->start(), 0))) {
+    return nullptr;
+  }
+  if (!ranges->Insert(new MemoryRange(process_memory, start(), end() - start(), elf_offset()))) {
+    return nullptr;
+  }
+  return ranges.release();
 }
 
 class ScopedElfCacheLock {
diff --git a/libunwindstack/Memory.cpp b/libunwindstack/Memory.cpp
index b058530..5cfe2f0 100644
--- a/libunwindstack/Memory.cpp
+++ b/libunwindstack/Memory.cpp
@@ -369,7 +369,7 @@
   return memory_->Read(read_addr, dst, read_length);
 }
 
-void MemoryRanges::Insert(MemoryRange* memory) {
+bool MemoryRanges::Insert(MemoryRange* memory) {
   uint64_t last_addr;
   if (__builtin_add_overflow(memory->offset(), memory->length(), &last_addr)) {
     // This should never happen in the real world. However, it is possible
@@ -378,7 +378,12 @@
     // value.
     last_addr = UINT64_MAX;
   }
-  maps_.emplace(last_addr, memory);
+  auto entry = maps_.try_emplace(last_addr, memory);
+  if (entry.second) {
+    return true;
+  }
+  delete memory;
+  return false;
 }
 
 size_t MemoryRanges::Read(uint64_t addr, void* dst, size_t size) {
diff --git a/libunwindstack/MemoryRange.h b/libunwindstack/MemoryRange.h
index 3b4ab5c..ac0558b 100644
--- a/libunwindstack/MemoryRange.h
+++ b/libunwindstack/MemoryRange.h
@@ -53,7 +53,7 @@
   MemoryRanges() = default;
   virtual ~MemoryRanges() = default;
 
-  void Insert(MemoryRange* memory);
+  bool Insert(MemoryRange* memory);
 
   size_t Read(uint64_t addr, void* dst, size_t size) override;
 
diff --git a/libunwindstack/tests/ElfTest.cpp b/libunwindstack/tests/ElfTest.cpp
index 77a94be..f16c6e5 100644
--- a/libunwindstack/tests/ElfTest.cpp
+++ b/libunwindstack/tests/ElfTest.cpp
@@ -551,12 +551,12 @@
   EXPECT_EQ(0x1000U, elf.GetLastErrorAddress());
 }
 
-TEST_F(ElfTest, get_printable_build_id_empty) {
+TEST(ElfBuildIdTest, get_printable_build_id_empty) {
   std::string empty;
   ASSERT_EQ("", Elf::GetPrintableBuildID(empty));
 }
 
-TEST_F(ElfTest, get_printable_build_id_check) {
+TEST(ElfBuildIdTest, get_printable_build_id_check) {
   std::string empty = {'\xff', '\x45', '\x40', '\x0f'};
   ASSERT_EQ("ff45400f", Elf::GetPrintableBuildID(empty));
 }
diff --git a/libunwindstack/tests/MemoryRangesTest.cpp b/libunwindstack/tests/MemoryRangesTest.cpp
index c6c909a..cf3250a 100644
--- a/libunwindstack/tests/MemoryRangesTest.cpp
+++ b/libunwindstack/tests/MemoryRangesTest.cpp
@@ -86,4 +86,10 @@
   }
 }
 
+TEST_F(MemoryRangesTest, duplicate_last_addr) {
+  MemoryRanges ranges;
+  ASSERT_TRUE(ranges.Insert(new MemoryRange(nullptr, 0x1000, 0x2000, 0x1000)));
+  ASSERT_FALSE(ranges.Insert(new MemoryRange(nullptr, 0x2000, 0x1000, 0x2000)));
+}
+
 }  // namespace unwindstack