From ce34d623512787126f6bd02ed32e25d46789d490 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Wed, 30 Jan 2019 10:55:27 -0800 Subject: [PATCH] Avoid signed extension of chars for build ids. Added a unit test that fails before the change and passes afterwards. Bug: 120606663 Test: All unit tests pass. Change-Id: I054c7eac0c55abc3babe1d48a041f5819ad9db81 --- libunwindstack/MapInfo.cpp | 3 ++- libunwindstack/tests/MapInfoGetBuildIDTest.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/libunwindstack/MapInfo.cpp b/libunwindstack/MapInfo.cpp index 9e6bcd4e3..89a6a79c9 100644 --- a/libunwindstack/MapInfo.cpp +++ b/libunwindstack/MapInfo.cpp @@ -320,7 +320,8 @@ std::string MapInfo::GetPrintableBuildID() { } 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 ea9befcda..16451d115 100644 --- a/libunwindstack/tests/MapInfoGetBuildIDTest.cpp +++ b/libunwindstack/tests/MapInfoGetBuildIDTest.cpp @@ -78,6 +78,17 @@ TEST_F(MapInfoGetBuildIDTest, from_elf) { 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(0xfa), static_cast(0xab), static_cast(0x12), + static_cast(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;