From 120504f52b5feedcb9ad7dedc54d7618a2f2c31b Mon Sep 17 00:00:00 2001 From: Jeff Hao Date: Fri, 14 Apr 2017 14:33:52 -0700 Subject: [PATCH] Add check that TypeIndex is valid in StringByTypeIdx. StringByTypeIdx should fail gracefully if given a bad TypeIndex. This adds a check that the TypeIndex is valid before getting its TypeId. This fixes a regression that removed this check when it was refactored in this CL: https://android-review.googlesource.com/#/c/243493/ Bug: 37287051 Test: mm -j31 test-art-host-gtest-dex_file_test Change-Id: Ib68cb8135011f5f30335251583e181b089982754 --- runtime/dex_file-inl.h | 6 ++++++ runtime/dex_file_test.cc | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/runtime/dex_file-inl.h b/runtime/dex_file-inl.h index e884e39c1..41db4d821 100644 --- a/runtime/dex_file-inl.h +++ b/runtime/dex_file-inl.h @@ -59,11 +59,17 @@ inline const char* DexFile::StringDataByIdx(dex::StringIndex idx) const { } inline const char* DexFile::StringByTypeIdx(dex::TypeIndex idx, uint32_t* unicode_length) const { + if (!idx.IsValid()) { + return nullptr; + } const TypeId& type_id = GetTypeId(idx); return StringDataAndUtf16LengthByIdx(type_id.descriptor_idx_, unicode_length); } inline const char* DexFile::StringByTypeIdx(dex::TypeIndex idx) const { + if (!idx.IsValid()) { + return nullptr; + } const TypeId& type_id = GetTypeId(idx); return StringDataByIdx(type_id.descriptor_idx_); } diff --git a/runtime/dex_file_test.cc b/runtime/dex_file_test.cc index 3f7461c30..f811287a9 100644 --- a/runtime/dex_file_test.cc +++ b/runtime/dex_file_test.cc @@ -591,4 +591,11 @@ TEST_F(DexFileTest, OpenDexBadMapOffset) { EXPECT_EQ(raw, nullptr); } +TEST_F(DexFileTest, GetStringWithNoIndex) { + ScratchFile tmp; + std::unique_ptr raw(OpenDexFileBase64(kRawDex, tmp.GetFilename().c_str())); + dex::TypeIndex idx; + EXPECT_EQ(raw->StringByTypeIdx(idx), nullptr); +} + } // namespace art -- 2.11.0