Merge "Avoid signed extension of chars for build ids." am: d451f39245 am: 959013f3bd
am: a050cd0669

Change-Id: Ied303553db98450ed7607de99ffac2668a1d5d85
diff --git a/libunwindstack/MapInfo.cpp b/libunwindstack/MapInfo.cpp
index 9e6bcd4..89a6a79 100644
--- a/libunwindstack/MapInfo.cpp
+++ b/libunwindstack/MapInfo.cpp
@@ -320,7 +320,8 @@
   }
   std::string printable_build_id;
   for (const char& c : raw_build_id) {
-    printable_build_id += android::base::StringPrintf("%02x", c);
+    // Use %hhx to avoid sign extension on abis that have signed chars.
+    printable_build_id += android::base::StringPrintf("%02hhx", c);
   }
   return printable_build_id;
 }
diff --git a/libunwindstack/tests/MapInfoGetBuildIDTest.cpp b/libunwindstack/tests/MapInfoGetBuildIDTest.cpp
index ea9befc..16451d1 100644
--- a/libunwindstack/tests/MapInfoGetBuildIDTest.cpp
+++ b/libunwindstack/tests/MapInfoGetBuildIDTest.cpp
@@ -78,6 +78,17 @@
   EXPECT_EQ("46414b455f4255494c445f4944", map_info_->GetPrintableBuildID());
 }
 
+TEST_F(MapInfoGetBuildIDTest, from_elf_no_sign_extension) {
+  map_info_->elf.reset(elf_container_.release());
+
+  std::string build_id = {static_cast<char>(0xfa), static_cast<char>(0xab), static_cast<char>(0x12),
+                          static_cast<char>(0x02)};
+  elf_interface_->FakeSetBuildID(build_id);
+
+  EXPECT_EQ("\xFA\xAB\x12\x2", map_info_->GetBuildID());
+  EXPECT_EQ("faab1202", map_info_->GetPrintableBuildID());
+}
+
 void MapInfoGetBuildIDTest::MultipleThreadTest(std::string expected_build_id) {
   static constexpr size_t kNumConcurrentThreads = 100;